-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
29 lines (25 loc) · 1.15 KB
/
script.js
File metadata and controls
29 lines (25 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const result = document.getElementById("result");
const message = document.getElementById("message");
const choices = ["rock", "paper", "scissors"];
function play (event) {
const userChoice = event.target.id;
message.innerHTML = "You selected " + userChoice + "!" + "<br/>";
const randomNumber = Math.floor(Math.random() * choices.length);
const computerChoice = choices[randomNumber];
message.innerHTML += "The computer chose " + computerChoice + "!";
if (computerChoice === userChoice) {
result.innerHTML = "Draw!";
} else if (computerChoice === "rock" && userChoice === "paper") {
result.innerHTML = "You win!";
} else if (computerChoice === "rock" && userChoice === "scissors") {
result.innerHTML = "You lost!";
} else if (computerChoice === "paper" && userChoice === "scissors") {
result.innerHTML = "You win!";
} else if (computerChoice === "paper" && userChoice === "rock") {
result.innerHTML = "You lose!";
} else if (computerChoice === "scissors" && userChoice === "rock") {
result.innerHTML = "You win!";
} else if (computerChoice === "scissors" && userChoice === "paper") {
result.innerHTML = "You lose!";
}
}