-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
39 lines (34 loc) · 1.31 KB
/
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
30
31
32
33
34
35
36
37
38
39
document.querySelectorAll('.choice').forEach(item => {
item.addEventListener('click', event => {
event.preventDefault();
const playerChoice = event.target.id;
const computerChoice = getComputerChoice();
const result = determineWinner(playerChoice, computerChoice);
const resultElement = document.getElementById('result');
resultElement.textContent = `You chose ${playerChoice}, Computer chose ${computerChoice}. ${result}`;
// Add animation class
resultElement.classList.add('result-animation');
// Remove the animation class after animation ends
setTimeout(() => {
resultElement.classList.remove('result-animation');
}, 500);
});
});
function getComputerChoice() {
const choices = ['rock', 'paper', 'scissor'];
const randomIndex = Math.floor(Math.random() * choices.length);
return choices[randomIndex];
}
function determineWinner(player, computer) {
if (player === computer) {
return 'It\'s a tie!';
}
if (
(player === 'rock' && computer === 'scissor') ||
(player === 'scissor' && computer === 'paper') ||
(player === 'paper' && computer === 'rock')
) {
return 'You win!';
}
return 'You lose!';
}