Skip to main content

JavaScript Math Rounding — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Math Rounding 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 the nearest

    easy10 min

    Report four point five rounded.

  2. 2

    Always round down

    easy10 min

    Report four point nine rounded down.

  3. 3

    Always round up

    easy10 min

    Report four point one rounded up.

  4. 4

    Count the pages needed

    easy10 min

    Work out how many pages hold 23 items at ten per page.

  5. 5

    Round to two decimals

    medium10 min

    Round pi to two decimal places as a number.

Find the bug(5)

  1. 1

    Fix floor used for pages

    easy8 min

    Rounding down loses the last partial page.

  2. 2

    Fix the negative rounded wrongly

    easy8 min

    floor goes further down on a negative; trunc just cuts.

  3. 3

    Fix the decimal rounding

    easy8 min

    Multiply before rounding, then divide back.

  4. 4

    Fix ceil used where floor belongs

    easy8 min

    Whole units should round down, not up.

  5. 5

    Fix toFixed used where a number is needed

    easy8 min

    toFixed returns a string, so the arithmetic joins instead.

Ready to write the code?

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

Open the workspace