Skip to main content

JavaScript Type Conversion — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Type Conversion 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

    Convert text to a number

    easy10 min

    Convert the text and add eight to it.

  2. 2

    Convert a number to text

    easy10 min

    Convert a number to a string and report its type.

  3. 3

    Convert to a boolean

    easy10 min

    Report what Boolean gives for an empty string.

  4. 4

    Handle a failed conversion

    easy10 min

    Report a message when the text cannot become a number.

  5. 5

    Add two form values

    medium10 min

    Convert both inputs before adding them.

Find the bug(5)

  1. 1

    Fix the values joined instead of added

    easy8 min

    Text values join with plus rather than adding.

  2. 2

    Fix the conversion applied too late

    easy8 min

    The join has already happened before the conversion.

  3. 3

    Fix the failed conversion unchecked

    easy8 min

    A failed conversion gives NaN, which must be checked for.

  4. 4

    Fix toString called on null

    easy8 min

    String works on null, but toString throws.

  5. 5

    Fix the empty string assumed to be NaN

    easy8 min

    Number of an empty string is 0, not NaN.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace