Checking by hand
JavaScript · Unit 20: Testing and debugging
- Full plan
- JavaScript
- 10 exercises
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. Predict the output
Somebody wrote
doublein a hurry. What do the two tests print?2. Complete the code
half(10)should give 5. Complete the test so it printstrue.3. Predict the output
What does this code print?
4. Put the lines in order
Put
check(t, got, exp)in order: it printsokand the test name when what you got equals what you expected, andBADand the name when it doesn't.5. Multiple choice
Why is this test useless?
6. Complete the code
The test doesn't match (10 / 2 is 5, not 4). Complete it so the
catchpicks it up.7. Predict the output
What does this code print?
8. Find the bug
checkshould printBADwhen what you got isn't what you expected. With3 * 3and 6 it printsok. Which line has the bug?9. Find the bug
readAgeshould give back a number and it gives back the text"18". The test saysokanyway. Which line has the bug?10. Predict the output
Both tests check
bigger. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.