17·6
2016

In part 2 of Lesson 2 you get to build a little rock, paper, scissors game. It introduces a new item - the *random* method on *Math*.

`math.random()`

I was curious, because they don't really explain it. Just looking at the command itself it looks like it has two parts `math`

and `random`

. I kind of assume that means it's a command of the type "math", and its specific function is creating a random number. Turns out I'm right: it's called a method and in this case it picks a number between 0 and 1.

For this game you use a clever comparison to determine a selection out of 3 using <= 0.33, <= 0.66 and <=1. We change it later when we add "rope" as an option.

### Ultimately this is the little game you get to write at the end of lesson 2. Keeping it here for the record.

```
var userChoice = prompt("Do you choose rock, paper, scissors or rope?");
var computerChoice = Math.random();
if (computerChoice < 0.25) {
computerChoice = "rock";
} else if(computerChoice <= 0.40) {
computerChoice = "paper";
} else if(computerChoice <= 0.75) {
computerChoice = "scissors";
} else {
computerChoice = "rope";
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
console.log("not sure how to handle this");
}
else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
}
else if (choice2 === "paper") {
return "paper wins";
}
else {
return "rope wins";
}
}
else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
}
else if (choice2 === "scissors") {
return "scissors win";
}
else {
return "rope wins";
}
}
else if (choice1 === "scissors") {
if (choice2 === "paper") {
return "scissors win";
}
if (choice2 === "rock") {
return "rock wins";
}
else {
return "rope wins";
}
}
else if (choice1 === "rope") {
if (choice2 === "rock") {
return "rope wins";
}
if (choice2 === "paper") {
return "paper wins";
}
else {
return "scissors win";
}
}
else {
return "that's not fair! pick rock, paper, scissors or rope!";
}
}
compare(userChoice,computerChoice)
```