Convert text that is not a number and report the result.
Acceptance criteria
- 1.#out should show NaN.
JavaScript Tutorial · JavaScript Type System
These 10 tasks go with the JavaScript NaN chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “NaN means Not a Number, and it is the result of a failed number operation.” Read the chapter.
1.Produce a NaNexercise · easy · 10 min
Convert text that is not a number and report the result.
Passes when: #out should show NaN.
2.Report its typeexercise · easy · 10 min · practice plan
Report what typeof gives for NaN.
Passes when: #out should show number.
3.Show it is not equal to itselfexercise · easy · 10 min · practice plan
Compare NaN with itself and report the outcome.
Passes when: #out should show not even itself.
4.Check for it properlyexercise · easy · 10 min · practice plan
Use Number.isNaN to detect a failed conversion.
Passes when: #out should show conversion failed.
5.Guard a calculationexercise · medium · 10 min · practice plan
Return a message rather than letting NaN escape.
Passes when: #out should show not a number.
6.Fix the equality check for NaNbug fix · easy · 8 min
NaN is never equal to itself, so equality cannot detect it.
Passes when: #out should show conversion failed.
7.Fix the global isNaNbug fix · easy · 8 min · practice plan
The global isNaN converts first, so it answers a different question.
Passes when: #out should show not the NaN value.
8.Fix the unguarded calculationbug fix · easy · 8 min · practice plan
A failed conversion spreads NaN through the whole sum.
Passes when: #out should show not a number.
9.Fix typeof used to detect NaNbug fix · easy · 8 min · practice plan
typeof NaN is number, so it cannot identify it.
Passes when: #out should show found NaN.
10.Fix the total ruined by one valuebug fix · easy · 8 min · practice plan
One unconvertible entry turns the whole total into NaN.
Passes when: #out should show 30.
Between them these tasks cover NaN, Number.isNaN and Failed conversion.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.