Javascript for Kids · Lesson

Rolling a Dice

Create a dice that gives 1 to 6.

Lesson 2 of 413 steps

Rolling a Dice is a free Javascript for Kids lesson on CoddyKit. This is 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, and your progress syncs across the web and the CoddyKit app. The Javascript for Kids course includes 4 lessons in total.

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 📜

  1. Times 6 for six numbers ✖️
  2. Floor to chop decimals 🧹
  3. 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! 🚀

Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
35
Lessons
140

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. 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. The Javascript for Kids course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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.

All lessons in this course

  1. Making Random Numbers
  2. Rolling a Dice
  3. Picking a Random Item
  4. A Lucky Number Game
← Back to Javascript for Kids