javascript
Game: rock, paper, scissors
Let's build a game! New tools:
const answer = prompt("Rock, paper or scissors? "); // waits for the player to type
const choices = ["rock", "paper", "scissors"];
const computer = choices[Math.floor(Math.random() * 3)]; // random pick
Math.random() gives a random number between 0 and 1. Multiply by 3 and round down with Math.floor, and you get 0, 1 or 2: a random position in the array.
In JavaScript, if looks like this:
if (answer === computer) {
console.log("Draw!");
} else if (answer === "rock" && computer === "scissors") {
console.log("You win!");
} else {
console.log("...");
}
=== means "equal", && means "and", || means "or".
Your mission
No automatic check, it's your game! Finish it, then try:
- Best of 3 rounds, with a score.
- Accept
R,P,Sas shortcuts. - Add "lizard" and "Spock" (look up the rules 🖖).