Skip to main content

JavaScript Number Precision — exercises and bug fixes

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

    See the precision error

    easy10 min

    Report whether adding a tenth to two tenths gives exactly three tenths.

  2. 2

    Compare with a tolerance

    easy10 min

    Check the difference is small enough instead.

  3. 3

    Round for display

    easy10 min

    Show the total rounded to two decimal places.

  4. 4

    Work in whole numbers

    easy10 min

    Add the values as pence and convert back to pounds.

  5. 5

    Show whole numbers are exact

    medium10 min

    Report whether one plus two is exactly three.

Find the bug(5)

  1. 1

    Fix the exact decimal comparison

    easy8 min

    Decimals should be compared with a tolerance.

  2. 2

    Fix the accumulated drift

    easy8 min

    Adding a tenth ten times does not give exactly one.

  3. 3

    Fix the money stored as pounds

    easy8 min

    Storing pence keeps the arithmetic exact.

  4. 4

    Fix the tolerance that is too tight

    easy8 min

    A tolerance of zero is the same as an exact comparison.

  5. 5

    Fix the missing absolute value

    easy8 min

    Without abs, a negative difference slips under any tolerance.

Ready to write the code?

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

Open the workspace