Rolling a Dice
Create a dice that gives 1 to 6.
Rolling a Dice is a free Javascript for Kids lesson on CoddyKit — lesson 2 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.
Roll the Dice! 🎲
Let us make a REAL dice that rolls 1 to 6! 🎲
Perfect for board games! 🎮
The Decimal Problem 🤔
Math.random() gives messy decimals like 0.73. 😅
A dice needs whole numbers! We need a cleanup spell. 🧹
Meet Math.floor() 🧹
Math.floor() chops off the decimals and rounds DOWN! 👇
console.log(Math.floor(4.9));
console.log(Math.floor(2.1));Floor Always Goes Down ⬇️
Even 4.9 becomes 4! Floor always goes to the lower whole number. 🪜
Like walking down stairs! 🚶
Step 1: Six Numbers 6️⃣
Multiply random by 6 to get numbers from 0 up to almost 6. 🔢
console.log(Math.random() * 6);Step 2: Chop Decimals ✂️
Add Math.floor() to get whole numbers 0 to 5! 🧹
console.log(Math.floor(Math.random() * 6));Step 3: Add 1! ➕
Dice start at 1, not 0! So we add 1 to get 1 to 6. 🎲
console.log(Math.floor(Math.random() * 6) + 1);Roll It Many Times! 🔁
Press Run again and again. A new roll every time! 🎲🎲
console.log(Math.floor(Math.random() * 6) + 1);
console.log(Math.floor(Math.random() * 6) + 1);Make a Dice Spell 🪄
Let us put our dice in a function so we can roll any time! ✨
function rollDice() {
return Math.floor(Math.random() * 6) + 1;
}
console.log(rollDice());The Magic Recipe 📜
- Times 6 for six numbers ✖️
- Floor to chop decimals 🧹
- Plus 1 to start at one ➕
You Built a Dice! 🎲
Awesome! You made a real dice from 1 to 6! 🎉
Now you can build dice games!
Quick Check 🧠
Question time! 🎯
Dice Master! 🏆
Hooray! You can roll dice in code! 🎲
Next, we pick random items from a list! 🚀
Frequently asked questions
Is the “Rolling a Dice” lesson free?
Yes — the full text of “Rolling a Dice” 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 Dice”?
Create a dice that gives 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Rolling a Dice” 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.