-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
22 lines (19 loc) · 999 Bytes
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const message = document.getElementById('message');
document.getElementById('rock').addEventListener('click', () => playGame('rock'));
document.getElementById('paper').addEventListener('click', () => playGame('paper'));
document.getElementById('scissors').addEventListener('click', () => playGame('scissors'));
function playGame(playerChoice) {
const choices = ['rock', 'paper', 'scissors'];
const computerChoice = choices[Math.floor(Math.random() * 3)];
if (playerChoice === computerChoice) {
message.textContent = `It's a tie! Both chose ${playerChoice}.`;
} else if (
(playerChoice === 'rock' && computerChoice === 'scissors') ||
(playerChoice === 'paper' && computerChoice === 'rock') ||
(playerChoice === 'scissors' && computerChoice === 'paper')
) {
message.textContent = `You win! ${playerChoice} beats ${computerChoice}.`;
} else {
message.textContent = `You lose! ${computerChoice} beats ${playerChoice}.`;
}
}