Write a function called rollDice that works like
rolling a real dice: every time you call it, it gives back a random whole number
from 1 to 6.
rollDicerollDice() ➜ 4
rollDice() ➜ 1
rollDice() ➜ 6
Try the finished version first so you know what you're building: open the Dice Roller demo ➜
Math.random() gives a random decimal between 0 and 1.
Math.random() * 6 gives a decimal between 0 and 6, like 3.7
Math.floor(...) chops off the decimal part:
Math.floor(3.7) becomes 3.
So Math.floor(Math.random() * 6) gives 0, 1, 2, 3, 4 or 5.
Hmm... how do you turn 0–5 into 1–6? 🤔
function rollDice() {
return Math.floor(Math.random() * 6) + 1;
}