0Pricing
Javascript for Kids · Lesson

Random Answers

Pick a reply.

Random Answers 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.

The Magic 8-Ball! 🎱

Hi fortune teller! 🌟 Have you seen a Magic 8-Ball? You ask a question and it gives a surprise answer! 🎱

Today we make one in code! First step: picking a random answer! 🎲

A List of Answers 📋

First we need a list of replies! 📋

We store many answers in an array (a list in square brackets). 🪄

let answers = ['Yes!', 'No way!', 'Maybe...'];
console.log(answers);

Picking by Position 🔢

Each answer has a number spot called an index. 🔢

The first one is index 0! Let's grab it. 👆

let answers = ['Yes!', 'No way!', 'Maybe...'];
console.log(answers[0]);
console.log(answers[2]);

What is Random? 🎲

Math.random() gives a surprise number between 0 and 1! 🎲

It's different every time, like rolling dice! 😄

console.log(Math.random());
console.log(Math.random());

A Random Whole Number 🔢

We need a whole number to pick a spot! 🔢

Multiply by the list size and use Math.floor to round down. 🪄

let pick = Math.floor(Math.random() * 3);
console.log('Random spot: ' + pick);

Pick a Random Answer 🎯

Now let's grab the answer at that random spot! 🎯

Run it a few times to see different answers! 😄

let answers = ['Yes!', 'No way!', 'Maybe...'];
let pick = Math.floor(Math.random() * 3);
console.log(answers[pick]);

Using the List Length 📏

Instead of typing 3, use .length! 📏

That way it works no matter how many answers we have. 💡

let answers = ['Yes!', 'No', 'Maybe', 'Ask later'];
let pick = Math.floor(Math.random() * answers.length);
console.log(answers[pick]);

Roll Again and Again 🔁

Each time you run it, you get a surprise! 🔁

Let's pick three random answers in a row. 🎲🎲🎲

let answers = ['Yes!', 'No', 'Maybe', 'For sure!'];
for (let i = 0; i < 3; i++) {
  let pick = Math.floor(Math.random() * answers.length);
  console.log(answers[pick]);
}

A Pick Function 🤖

Let's make a function that picks a random answer! 🤖

Call it anytime to get a surprise reply! 🎱

let answers = ['Yes!', 'No', 'Maybe', 'Try again'];
function pickAnswer() {
  let pick = Math.floor(Math.random() * answers.length);
  return answers[pick];
}
console.log(pickAnswer());

Use Your Pick Machine 🎛️

Now we can get random answers super easily! 🎛️

Just call pickAnswer over and over! 🔁

let answers = ['Yes!', 'No', 'Maybe', 'Definitely'];
function pickAnswer() {
  let pick = Math.floor(Math.random() * answers.length);
  return answers[pick];
}
console.log(pickAnswer());
console.log(pickAnswer());

You Picked Random Answers! 🏆

Awesome work, fortune teller! 🏆 You learned to:

  • 📋 Store answers in an array
  • 🎲 Use Math.random and Math.floor
  • 🎯 Pick an answer by its index

Next lesson: revealing the fortune in style! ✨

Quick Check ✅

Brain time! 🧠 What helps us choose a surprise answer from the list?

Lesson Recap 🎀

Super job, 8-ball builder! 🎀 Today you learned:

  • 📋 Arrays hold many answers
  • 🎲 Math.random gives surprises
  • 🎯 Pick by index with Math.floor

Next: revealing the fortune! ✨

Frequently asked questions

Is the “Random Answers” lesson free?

Yes — the full text of “Random Answers” 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 “Random Answers”?

Pick a reply. 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 “Random Answers” 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. Asking a Question
  2. Random Answers
  3. Revealing the Fortune
  4. Add More Answers
← Back to Javascript for Kids