Skip to main content

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. 1

    Produce a NaN

    easy10 min

    Convert text that is not a number and report the result.

  2. 2

    Report its type

    easy10 min

    Report what typeof gives for NaN.

  3. 3

    Show it is not equal to itself

    easy10 min

    Compare NaN with itself and report the outcome.

  4. 4

    Check for it properly

    easy10 min

    Use Number.isNaN to detect a failed conversion.

  5. 5

    Guard a calculation

    medium10 min

    Return a message rather than letting NaN escape.

Find the bug(5)

  1. 1

    Fix the equality check for NaN

    easy8 min

    NaN is never equal to itself, so equality cannot detect it.

  2. 2

    Fix the global isNaN

    easy8 min

    The global isNaN converts first, so it answers a different question.

  3. 3

    Fix the unguarded calculation

    easy8 min

    A failed conversion spreads NaN through the whole sum.

  4. 4

    Fix typeof used to detect NaN

    easy8 min

    typeof NaN is number, so it cannot identify it.

  5. 5

    Fix the total ruined by one value

    easy8 min

    One 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