Your web browser does not support Java. Go to http://www.java.com to get Java.

Picture of a some dice being rolled

When you roll a die, you aren't sure what number you will get. You might get a one or a six or a five. This sort of number is called a random number.

random(8) will give you a random number between 1 and 8 (might be 2, might be 8, might be whatever).

Javascript has a command called "random" for making random numbers. With the random command, you must give a number between the brackets. Javascript will then give you a random number between 1 and the number you gave.

show(random(6));

In the program area on the left, type "show(random(6));" When you click the Run button, what number is shown? If you run the program several times, you should get lots of different numbers.

paper beats rock, rock beats scissors, scissors beats paper

Let's write a Rock, Paper, and Scissors game in Javascript. In this game, both you and the computer must choose between rock, paper, scissors. If your choice beats the computer's choice, then you win.

choose("What's Your Favorite Color", "red", "blue", "orange"); 
with a screen, showing how different choices give different numbers.

At the beginning of the program, you should choose whether you want rock, paper, or scissors. We'll use a special command called "choose." With the choose command, you first give a string with some instructions. Then, you put a comma and some more strings with different things that someone can choose. If you take the first string, choose will give you back a one. If you take the second string, choose will give you back a two. If you choose the third string, choose will give you back a three. And so forth.

rock = 1, paper = 2, scissors = 3
you = choose("Make your choice", "rock", "paper", "scissors");

So the program should start by asking you to choose rock, paper, or scissors. We stick the choice in a variable called "you." If "you" has 1, it means rock. If "you" has 2, it means paper. And if "you" has 3, it means scissors.


you = choose("Make your choice", "rock", "paper", "scissors");
me  = random(3);
if (me == 1) {show("rock");}
else if (me == 2) {show("paper");}
else if (me == 3) {show ("scissors");}

Then, the computer should choose rock, paper, or scissors. It uses the random() command to find a number between 1 and 3. It puts that number into the variable "me." It then tells you what its choice was. Remember that 1 means rock, 2 means paper, and 3 means scissors.


you = choose("Make your choice", "rock", "paper", "scissors");
me  = random(3);
if (me == 1) {show("rock");}
else if (me == 2) {show("paper");}
else if (me == 3) {show ("scissors");}
if (me == 1) {
  if (you == 1) {show("Draw");}
  else if (you == 2) {show("You win");}
  else if (you == 3) {show("I win");}

} else if (me == 2) {
  if (you == 1) {show("I win");}
  else if (you == 2) {show("Draw");}
  else if (you == 3) {show("You win");}

} else if (me == 3) {
  if (you == 1) {show("You win");}
  else if (you == 2) {show("I win");}
  else if (you == 3) {show("Draw");}
}

At then end, the program needs to figure out who won. It looks at your choice and the computer's choice to see who made the better choice. If "me" is 3 and "you" is 3, then both you and the computer chose scissors. That means nobody won, and the program will show the word draw. If "me" is 1 and "you" is 2, then the computer chose rock and you chose paper. That means you win. Try the program out

Sample output with score

Can you change the program so that you have to play the game three times, and the computer keeps a score?

Previous | Next

Programming Basics

E-mail | License | Privacy Policy