-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
28 lines (24 loc) · 903 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
// script.js
const choices = ['rock', 'paper', 'scissors'];
document.querySelectorAll('button').forEach(button => {
button.addEventListener('click', () => {
const playerChoice = button.id;
const computerChoice = choices[Math.floor(Math.random() * choices.length)];
const result = calculateWinner(playerChoice, computerChoice);
displayResult(result, computerChoice);
});
});
function calculateWinner(player, computer) {
if (player === computer) {
return 'It\'s a tie!';
} else if ((player === 'rock' && computer === 'scissors') ||
(player === 'paper' && computer === 'rock') ||
(player === 'scissors' && computer === 'paper')) {
return 'You win!';
} else {
return 'Computer wins!';
}
}
function displayResult(result, computerChoice) {
document.getElementById('result').textContent = `${result} Computer chose ${computerChoice}.`;
}