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
Check the basic range
easy10 minReport that a random value falls from zero up to one.
- 2
Make a whole number
easy10 minProduce a whole number from zero to nine and check it.
- 3
Roll a dice
easy10 minProduce a value from one to six and check it.
- 4
Use any range
easy10 minWrite a function giving a value between ten and twenty inclusive.
- 5
Pick from an array
medium10 minChoose a random colour and confirm it came from the list.
Find the bug(5)
- 1
Fix the missing floor
easy8 minWithout floor the dice value is a decimal.
- 2
Fix round used instead of floor
easy8 minRounding can reach ten, which is outside the wanted range.
- 3
Fix the range missing its top value
easy8 minWithout the plus one the maximum can never appear.
- 4
Fix the index off the end
easy8 minMultiplying by the length gives a valid index only after flooring.
- 5
Fix the minimum never added
easy8 minWithout 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