0Pricing
Javascript for Kids · Lesson

A Lucky Number Game

Build a tiny game of chance.

A Lucky Number Game is a free Javascript for Kids lesson on CoddyKit — lesson 4 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.

A Lucky Game! 🍀

Let us build a tiny game of chance! 🎰

The computer picks a lucky number. Did you win? 🎉

The Lucky Number 🎲

First, the computer picks a secret lucky number from 1 to 6. 🍀

let lucky = Math.floor(Math.random() * 6) + 1;
console.log("Lucky number chosen!");

Your Guess 🙋

Now YOU pick a number. We save it in a box! ✍️

let lucky = 4;
let guess = 4;
console.log("You guessed " + guess);

Did You Win? 🤔

We compare your guess to the lucky number with ===! 🟰

let lucky = 4;
let guess = 4;
console.log(guess === lucky);

Win Message! 🎉

Let us show a happy message if you win! 😄

let lucky = 4;
let guess = 4;
if (guess === lucky) {
  console.log("You win! Lucky you!");
}

Lose Message 😅

And a try-again message if you miss. No worries! 💪

let lucky = 4;
let guess = 2;
if (guess === lucky) {
  console.log("You win!");
} else {
  console.log("Aw, try again!");
}

Make It Truly Random 🎰

Now use real random for the lucky number! It is a real game! 🎲

let lucky = Math.floor(Math.random() * 6) + 1;
let guess = 3;
if (guess === lucky) {
  console.log("You win!");
} else {
  console.log("Try again! Lucky was " + lucky);
}

Put It in a Spell 🪄

Let us make a function so we can play again and again! ✨

function play(guess) {
  let lucky = Math.floor(Math.random() * 6) + 1;
  if (guess === lucky) {
    return "You win!";
  }
  return "Try again! Lucky was " + lucky;
}
console.log(play(3));

Play Many Rounds! 🔁

Call your game spell with different guesses! 🎮

function play(guess) {
  let lucky = Math.floor(Math.random() * 6) + 1;
  return guess === lucky ? "Win!" : "Try again!";
}
console.log(play(2));
console.log(play(5));

Look What You Made! 🏗️

You used random, comparisons, if/else, AND functions! 🤝

That is a real game. You are a game maker! 🎮

So Proud of You! 🌟

Every tool you learned worked together here. 🧰

Keep building and your games will get bigger! 🚀

Quick Check 🧠

Last question for this course! 🎯

Game Maker! 🏆

Hooray! You built a lucky number game! 🍀

Next, a bigger mini game: Number Guessing! 🚀

Frequently asked questions

Is the “A Lucky Number Game” lesson free?

Yes — the full text of “A Lucky Number Game” 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 “A Lucky Number Game”?

Build a tiny game of chance. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “A Lucky Number Game” 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