Git Product home page Git Product logo

functions-lab's Introduction

Functions Lab

Practice writing code to make tests pass!

Getting Started

  • Fork this repo
  • Clone the forked repository
  • cd to the directory where you cloned it
  • npm install to install dependencies
  • npm test to run the tests

Directions

This lab is designed to introduce you to the concept of writing code in a specific way, in order to make automated tests pass.

Once you have followed the setup instructions, look in the problems folder.

Each file in the problems folder contains a function that you must fill out to make the test pass. Above the function is a description of the function, the parameters it needs, and the return value.

Write your code in the function, then run npm test to check your work. When you're first starting out, all the tests will fail.

By default, npm test runs every test file, so you will have to scroll up to see all the output. See Reading the test output below for more info.

When writing your code, DO NOT change the name of the function, the filename, mess with the exports, or else the tests will not work.

The problem files are numbered, starting with 1, and we suggest doing them in that order.

Running Single Test Files

If you want to reduce the amount of output you get, you can test only a single file at a time.

Simply run npm test and then the name of the test file you want to run.

So to test the multiply problems, run:

npm test __tests__/multiply.test.js

Bonus Questions

Bonus questions are in the /bonus folder. To run the tests you can simply do npm run bonus

Submission requirements

  • When finished, commit your work.
  • Make a pull request on github.

If all your tests pass when you run npm test, then you should see a notice in the pull request that they all passed. If even one test fails on your computer, it will look like they all failed on github.

Reading the function description

Here is a sample description and function:

/**
 * Takes in two values and returns their average

 * @param {number} a - The first number
 * @param {number} b - The second number
 * @returns {number} - a number equal to the average of a and b
 */

function average() {

}

The first line describes, in a plain way, what the function should do.

The params contain the type and the suggested name of the parameters. In this case both are numbers, not strings, arrays, or objects. The first is named a and the second named b.

The returns line describes the type of value that should be returned from the function, and what it should be. So if it says number and you return a string then the test will fail.

Notice that the function is written without any parameters - you must add them in based on the description!

Reading the test output

When you run npm test you will see something like this:

 FAIL  __tests__/sum.test.js
  โ— adds 1 + 2 to equal 3

    expect(received).toBe(expected) // Object.is equality

    Expected: 3
    Received: undefined

      2 | 
      3 | test('adds 1 + 2 to equal 3', () => {
    > 4 |   expect(sum(1,2)).toBe(3)
        |                    ^
      5 | })
      6 | 

      at Object.<anonymous> (__tests__/sum.test.js:4:20)

Test Suites: 2 failed, 1 passed, 3 total

The first line tells you which test file the message corresponds to. So if the file is named sum.test.js then it's probably referring to code you wrote in sum.js

The second line is the description of what the test is looking for.

The Expected and Received lines show what the test was hoping to get, and what it did get back. If it says it received something undefined you probably forgot to return inside the function.

Below that, the code shows what the test case does. It literally imports your function and tries to run it - so in this case, it runs sum(1, 2) and expects the function to return a 3.

Each test file may contain multiple tests! And each problem file may contain multiple functions, so make sure you read all the test output.

functions-lab's People

Contributors

benstone1 avatar tcarina avatar asadhh avatar myrasmith avatar coreyladovsky avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.