Skip to main content

JavaScript Math.random — exercises and bug fixes

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

    Check the basic range

    easy10 min

    Report that a random value falls from zero up to one.

  2. 2

    Make a whole number

    easy10 min

    Produce a whole number from zero to nine and check it.

  3. 3

    Roll a dice

    easy10 min

    Produce a value from one to six and check it.

  4. 4

    Use any range

    easy10 min

    Write a function giving a value between ten and twenty inclusive.

  5. 5

    Pick from an array

    medium10 min

    Choose a random colour and confirm it came from the list.

Find the bug(5)

  1. 1

    Fix the missing floor

    easy8 min

    Without floor the dice value is a decimal.

  2. 2

    Fix round used instead of floor

    easy8 min

    Rounding can reach ten, which is outside the wanted range.

  3. 3

    Fix the range missing its top value

    easy8 min

    Without the plus one the maximum can never appear.

  4. 4

    Fix the index off the end

    easy8 min

    Multiplying by the length gives a valid index only after flooring.

  5. 5

    Fix the minimum never added

    easy8 min

    Without adding the minimum the range starts at zero.

Ready to write the code?

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

Open the workspace