JavaScript NaN — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript NaN lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Produce a NaN
easy10 minConvert text that is not a number and report the result.
- 2
Report its type
easy10 minReport what typeof gives for NaN.
- 3
Show it is not equal to itself
easy10 minCompare NaN with itself and report the outcome.
- 4
Check for it properly
easy10 minUse Number.isNaN to detect a failed conversion.
- 5
Guard a calculation
medium10 minReturn a message rather than letting NaN escape.
Find the bug(5)
- 1
Fix the equality check for NaN
easy8 minNaN is never equal to itself, so equality cannot detect it.
- 2
Fix the global isNaN
easy8 minThe global isNaN converts first, so it answers a different question.
- 3
Fix the unguarded calculation
easy8 minA failed conversion spreads NaN through the whole sum.
- 4
Fix typeof used to detect NaN
easy8 mintypeof NaN is number, so it cannot identify it.
- 5
Fix the total ruined by one value
easy8 minOne unconvertible entry turns the whole total into NaN.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace