⬅ Back to homework list

🎲 HW 2: Dice Roller

🎯 Your Mission

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.

Example: rollDice()4   rollDice()1   rollDice()6

👀 See it working

Try the finished version first so you know what you're building: open the Dice Roller demo ➜

💻 Your Code

📊 Results

Press "Test my code!" to see your score.

🔍 Stuck? Open a hint

Hint 1

Math.random() gives a random decimal between 0 and 1.
Math.random() * 6 gives a decimal between 0 and 6, like 3.7

Hint 2

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? 🤔

🆘 Show me one answer

function rollDice() {
  return Math.floor(Math.random() * 6) + 1;
}