Skip to the content

Checking by hand

JavaScript · Unit 20: Testing and debugging

Testing a function doesn't need a separate program: it's calling it with data you know and comparing what came out with what you expected.

That true is your first test.

function double(n) {
  return n * 2;
}
console.log(double(5) === 10);

Prints

true

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    Somebody wrote double in a hurry. What do the two tests print?

  2. 2. Complete the code

    half(10) should give 5. Complete the test so it prints true.

  3. 3. Predict the output

    What does this code print?

  4. 4. Put the lines in order

    Put check(t, got, exp) in order: it prints ok and the test name when what you got equals what you expected, and BAD and the name when it doesn't.

  5. 5. Multiple choice

    Why is this test useless?

  6. 6. Complete the code

    The test doesn't match (10 / 2 is 5, not 4). Complete it so the catch picks it up.

  7. 7. Predict the output

    What does this code print?

  8. 8. Find the bug

    check should print BAD when what you got isn't what you expected. With 3 * 3 and 6 it prints ok. Which line has the bug?

  9. 9. Find the bug

    readAge should give back a number and it gives back the text "18". The test says ok anyway. Which line has the bug?

  10. 10. Predict the output

    Both tests check bigger. What does it print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →