Skip to main content

JavaScript parseInt and parseFloat — exercises and bug fixes

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

    Read a number from text

    easy10 min

    Pull the number out of a CSS width.

  2. 2

    Keep the decimal part

    easy10 min

    Read the price from text that has words after it.

  3. 3

    Use a different base

    easy10 min

    Read a hexadecimal value.

  4. 4

    Handle unreadable text

    easy10 min

    Report a message when nothing can be read.

  5. 5

    Compare with Number

    medium10 min

    Show that Number rejects what parseInt accepts.

Find the bug(5)

  1. 1

    Fix Number used on a suffixed value

    easy8 min

    Number needs the whole string to be numeric.

  2. 2

    Fix parseInt losing the decimals

    easy8 min

    parseInt truncates, so the pence disappear.

  3. 3

    Fix the missing radix

    easy8 min

    Hexadecimal text needs a base of sixteen.

  4. 4

    Fix the unchecked NaN

    easy8 min

    A failed parse gives NaN, which must be checked with Number.isNaN.

  5. 5

    Fix parseInt used for validation

    easy8 min

    parseInt accepts junk after the number, so it is too lenient here.

Ready to write the code?

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

Open the workspace