-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
26 lines (23 loc) · 944 Bytes
/
game.js
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
const options = document.querySelectorAll(".option");
const result = document.querySelector("#result");
options.forEach(option => {
option.addEventListener("click", function() {
const computerChoice = Math.floor(Math.random() * 3);
const choices = ["rock", "paper", "scissors"];
const computerChoiceText = choices[computerChoice];
const userChoiceText = this.id;
let message;
if (userChoiceText === computerChoiceText) {
message = "It's a tie!";
} else if (
(userChoiceText === "rock" && computerChoiceText === "scissors") ||
(userChoiceText === "paper" && computerChoiceText === "rock") ||
(userChoiceText === "scissors" && computerChoiceText === "paper")
) {
message = "You win!";
} else {
message = "You lose!";
}
result.textContent = `You chose ${userChoiceText}, the computer chose ${computerChoiceText}. ${message}`;
});
});