Report that a random value falls from zero up to one.
Acceptance criteria
- 1.#out should show in range.
JavaScript Tutorial · JavaScript Numbers & Math
These 10 tasks go with the JavaScript Math.random chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Math.random gives a random decimal between 0 and 1.” .
1.Check the basic rangeexercise · easy · 10 min
Report that a random value falls from zero up to one.
Passes when: #out should show in range.
2.Make a whole numberexercise · easy · 10 min · practice plan
Produce a whole number from zero to nine and check it.
Passes when: #out should show in range.
3.Roll a diceexercise · easy · 10 min · practice plan
Produce a value from one to six and check it.
Passes when: #out should show valid roll.
4.Use any rangeexercise · easy · 10 min · practice plan
Write a function giving a value between ten and twenty inclusive.
Passes when: #out should show in range.
5.Pick from an arrayexercise · medium · 10 min · practice plan
Choose a random colour and confirm it came from the list.
Passes when: #out should show picked one.
6.Fix the missing floorbug fix · easy · 8 min
Without floor the dice value is a decimal.
Passes when: #out should show valid roll.
7.Fix round used instead of floorbug fix · easy · 8 min · practice plan
Rounding can reach ten, which is outside the wanted range.
Passes when: #out should show floor was used.
8.Fix the range missing its top valuebug fix · easy · 8 min · practice plan
Without the plus one the maximum can never appear.
Passes when: #out should show includes the top.
9.Fix the index off the endbug fix · easy · 8 min · practice plan
Multiplying by the length gives a valid index only after flooring.
Passes when: #out should show picked one.
10.Fix the minimum never addedbug fix · easy · 8 min · practice plan
Without adding the minimum the range starts at zero.
Passes when: #out should show in range.
Between them these tasks cover Math.random, Ranges and Random selection.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.