Mini Project 2: The Dice Roller

Grade 3, page 10 of 11. A one-minute read.

Let's roll two dice, add them up, and check for doubles. Run this several times. You'll get different results each time!

dice.js
const die1 = Math.floor(Math.random() * 6) + 1;
const die2 = Math.floor(Math.random() * 6) + 1;
const total = die1 + die2;
const isDoubles = die1 === die2;
const isEven = total % 2 === 0;

console.log(`🎲 You rolled a ${die1} and a ${die2}`);
console.log(`Total: ${total}`);
console.log(`Doubles? ${isDoubles}`);
console.log(`Even total? ${isEven}`);
console.log(`Lucky seven? ${total === 7}`);

Output

🎲 You rolled a 4 and a 4
Total: 8
Doubles? true
Even total? true
Lucky seven? false
Try it yourself

Change the dice roller to roll three dice. Then add a line that prints whether the total is greater than 10. Bonus: make one of the dice a 20-sided die (the kind used in board games).

That's Grade 3 in the books! Your programs can now calculate, compare and combine. But they still can't decide anything. That changes in Grade 4, when your code learns to make choices.

Reading is step one

CurlyScript turns these notes into a game: short questions, an explanation after every answer, and a test at the end of each grade. Free, with nothing to sign up for.

Start learning