Rolling a Die
Random 1 to 6.
Rolling a Die is a free Javascript for Kids lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Javascript for Kids learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Let's Roll Some Dice! 🎲
Welcome to the Roll the Dice game! 🎲
A dice (or "die") has six sides with numbers 1 to 6. We're going to roll one with code! 🎉
console.log('Rolling the dice! 🎲')
console.log('Numbers 1 to 6! 🔢')The Six Faces 🎲
A die has six faces:
- ⚀ one
- ⚁ two
- ⚂ three
- ⚃ four
- ⚄ five
- ⚅ six
When we roll, we get one of these! 🎯
console.log('A die has 6 sides ⚀⚁⚂⚃⚄⚅')Random Again! ✨
To roll, we need a random number. Remember Math.random()? ✨
It gives a number between 0 and 1. Let's use it for dice! 🎲
let r = Math.random()
console.log('Random: ' + r + ' ✨')Make It 0 to 5 🔢
If we multiply by 6 and round down, we get 0, 1, 2, 3, 4, or 5. 🔢
But dice start at 1, not 0... we'll fix that next! 😄
let n = Math.floor(Math.random() * 6)
console.log('Number 0 to 5: ' + n)Add 1 for Dice! ➕
Dice go from 1 to 6, not 0 to 5. So we just add 1! ➕
Now we get 1, 2, 3, 4, 5, or 6. A real dice roll! 🎲
let roll = Math.floor(Math.random() * 6) + 1
console.log('You rolled: ' + roll + ' 🎲')Roll Again and Again 🔁
Every run gives a new roll! 🎲 Sometimes 2, sometimes 6, sometimes 4!
It's a surprise every time. 😲
let roll = Math.floor(Math.random() * 6) + 1
console.log('This roll: ' + roll + ' 🎲')A Roll Function 🛠️
Let's make a little machine called rollDie. Call it anytime to roll! 🛠️
function rollDie() {
return Math.floor(Math.random() * 6) + 1
}
console.log('Roll: ' + rollDie() + ' 🎲')Three Rolls! 🎲🎲🎲
Now we can roll three times super easily! 🎲🎲🎲
Just call our machine three times. 🔂
function rollDie() {
return Math.floor(Math.random() * 6) + 1
}
console.log('Roll 1: ' + rollDie())
console.log('Roll 2: ' + rollDie())
console.log('Roll 3: ' + rollDie())Show a Dice Emoji 🎲
Let's make it fun! We can show the dice emoji with the number. 🎲
Roll and celebrate! 🎉
let roll = Math.floor(Math.random() * 6) + 1
console.log('🎲 You rolled a ' + roll + '! 🎉')Lucky Six? 🍀
Did you roll a six? That's lucky! 🍀
We can check with if and celebrate the big roll! 🎉
let roll = Math.floor(Math.random() * 6) + 1
if (roll === 6) {
console.log('Lucky six! 🍀🎉')
} else {
console.log('You rolled ' + roll + ' 🎲')
}You Can Roll Dice! 🎲
Yay! You learned to roll a die in code! 🎉
Math.random() * 6gives 0 to 5 🔢- Add
+ 1to get 1 to 6 ➕ - Wrap it in a function to reuse 🛠️
console.log('You can roll dice! ✅')
console.log('Next: two dice! 🎲🎲')Quick Check 🧠
Brain time! 🤔
Great Roll! 🎉
You can roll a die with code now! 🎲
- Random number 🔢
- Multiply by 6 ✖️
- Add 1 ➕
Next: let's roll two dice and add them up! 🎲🎲
console.log('Lesson done! ⭐')
console.log('Two dice next! 🎲🎲')Frequently asked questions
Is the “Rolling a Die” lesson free?
Yes — the full text of “Rolling a Die” is free to read here on the web, and the Javascript for Kids course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Javascript for Kids course, upgrade to CoddyKit PRO.
What will I learn in “Rolling a Die”?
Random 1 to 6. You practise Javascript for Kids with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Javascript for Kids?
No prior experience is required. Javascript for Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Rolling a Die” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Javascript for Kids lesson?
Yes. Every Javascript for Kids lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Rolling a Die
- Two Dice
- Tracking Dice Points
- First to Twenty