0Pricing
Javascript for Kids · Lesson

Computer Picks

Random choice.

Computer Picks 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 Robot Player 🤖

To play against the computer, the computer needs to pick rock, paper, or scissors too!

But how does a computer choose? It uses randomness! 🎲

console.log('The computer is thinking... 🤖')
console.log('It will pick something random! 🎲')

What is Random? 🎲

Random means a surprise! You don't know what you'll get. 😲

It's like spinning a wheel or rolling a dice. Anything can happen! 🌀

console.log('Random = a surprise! 🎉')
console.log('Like rolling a dice 🎲')

Math.random() ✨

JavaScript has a magic helper called Math.random().

It gives a random number between 0 and 1. Like 0.42 or 0.97! 🔢

let r = Math.random()
console.log('Random number: ' + r + ' ✨')

Picking 0, 1, or 2 🔢

We want one of three choices. So we multiply by 3 and round down with Math.floor().

This gives us 0, 1, or 2! 🎯

let n = Math.floor(Math.random() * 3)
console.log('Got number: ' + n + ' 🎯')

A List of Choices 📋

Let's put our three picks in an array. An array is a list! 📋

Position 0 is rock, 1 is paper, 2 is scissors.

let choices = ['rock', 'paper', 'scissors']
console.log(choices[0] + ' ✊')
console.log(choices[1] + ' 📄')
console.log(choices[2] + ' ✂️')

Pick From the List 🎰

Now we combine them! We pick a random number, then grab that spot in the list. 🎰

Magic! The computer made a choice! ✨

let choices = ['rock', 'paper', 'scissors']
let n = Math.floor(Math.random() * 3)
let computer = choices[n]
console.log('Computer picked: ' + computer + ' 🤖')

Run It Again! 🔁

Every time the code runs, the computer might pick something different! 🎲

Try running it many times. Sometimes rock, sometimes paper, sometimes scissors! 😄

let choices = ['rock', 'paper', 'scissors']
let computer = choices[Math.floor(Math.random() * 3)]
console.log('This time: ' + computer + ' 🎰')

A Helper Function 🛠️

Let's wrap our picking code in a function. A function is a little machine you can use again and again! 🛠️

function computerPick() {
  let choices = ['rock', 'paper', 'scissors']
  return choices[Math.floor(Math.random() * 3)]
}
console.log('Pick: ' + computerPick() + ' 🤖')

Use It Many Times 🔂

Now that we have our machine, we can call it as many times as we want! 🔂

Each call gives a fresh random pick. 🎲

function computerPick() {
  let choices = ['rock', 'paper', 'scissors']
  return choices[Math.floor(Math.random() * 3)]
}
console.log('Pick 1: ' + computerPick())
console.log('Pick 2: ' + computerPick())
console.log('Pick 3: ' + computerPick())

Show With an Emoji 😎

Let's make it pretty! We can show the pick with a fun emoji. ✊📄✂️

Now the computer's choice looks awesome! 🌈

let choices = ['rock ✊', 'paper 📄', 'scissors ✂️']
let computer = choices[Math.floor(Math.random() * 3)]
console.log('Computer chose: ' + computer)

You Made a Robot Pick! 🤖

Amazing! You taught the computer to pick! 🎉

  • Math.random() gives a surprise number 🎲
  • Math.floor() rounds it down 🔢
  • We use it to grab a choice from the list 📋
console.log('Computer can pick now! ✅')
console.log('Next: who wins? 🏆')

Quick Check 🧠

Let's see what you remember! 🤔

Awesome Work! 🎉

You taught the computer to pick rock, paper, or scissors! 🤖

  • Random number with Math.random() 🎲
  • Round down with Math.floor() 🔢
  • Grab a choice from the list 📋

Next: let's find out who wins! 🏆

console.log('Lesson complete! ⭐')
console.log('On to the winner! 🏆')

Frequently asked questions

Is the “Computer Picks” lesson free?

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

Random choice. 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 “Computer Picks” 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. The Rules
  2. Computer Picks
  3. Who Wins?
  4. Play Again
← Back to Javascript for Kids