Practical tools and techniques · Lesson 41 of 45
Testing and debugging JavaScript
Boundary cases, clear functions and result checking.
8 minutes1 games
Test expected behavior for known inputs, not only the absence of errors. A pure function returns a result from its inputs without changing external state, making testing easier. When debugging, reduce the example and inspect values with debugger or breakpoints.
JavaScript
function grade(score) {
return score >= 60 ? "Pass" : "Retry"
}
const cases = [[59, "Retry"], [60, "Pass"], [100, "Pass"]]
for (const [input, expected] of cases) {
const actual = grade(input)
if (actual !== expected) throw new Error(`Failed at ${input}`)
}
console.log("3 checks passed")| Tool | Use |
|---|---|
59 | Below boundary |
60 | At boundary |
100 | Higher case |
Exercise
Write clampScore(n) limiting a number to 0–100. Print results for -5, 60 and 120.
Expected output:
0 60 100
JavaScript
Learn more from
Played 0 of 1 games
Sign in to save your progress and earn XP.