-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
29 lines (25 loc) · 1015 Bytes
/
script.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
27
28
29
const options = ["rock", "paper", "scissors"];
const buttons = document.querySelectorAll('button');
const result = document.getElementById('result');
buttons.forEach((button) => {
button.addEventListener('click', playRound);
});
function computerPlay() {
const randomIndex = Math.floor(Math.random() * options.length);
return options[randomIndex];
}
function playRound(e) {
const playerSelection = e.target.id;
const computerSelection = computerPlay();
if (playerSelection === computerSelection) {
result.textContent = "It's a tie!";
} else if (
(playerSelection === "rock" && computerSelection === "scissors") ||
(playerSelection === "paper" && computerSelection === "rock") ||
(playerSelection === "scissors" && computerSelection === "paper")
) {
result.textContent = `You win! ${playerSelection} beats ${computerSelection}.`;
} else {
result.textContent = `You lose! ${computerSelection} beats ${playerSelection}.`;
}
}