Skip to main content

JavaScript Number Methods — exercises and bug fixes

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

    Round to two decimals

    easy10 min

    Format the price to two decimal places.

  2. 2

    Keep trailing zeros

    easy10 min

    Format a whole number as money.

  3. 3

    Convert to binary

    easy10 min

    Report 255 in base two.

  4. 4

    Convert to hex

    easy10 min

    Report 255 in base sixteen.

  5. 5

    Convert the result back

    medium10 min

    Round to two decimals then add one as a number.

Find the bug(5)

  1. 1

    Fix the string result added to

    easy8 min

    toFixed returns a string, so plus joins instead of adding.

  2. 2

    Fix round used instead of toFixed

    easy8 min

    Math.round loses the trailing zeros that money needs.

  3. 3

    Fix the missing base

    easy8 min

    Without a base, toString gives the ordinary decimal form.

  4. 4

    Fix toPrecision used for decimals

    easy8 min

    toPrecision counts all digits, not just the decimals.

  5. 5

    Fix the wrong number of decimals

    easy8 min

    Money needs two decimal places, not one.

Ready to write the code?

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

Open the workspace