-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
51 lines (42 loc) · 1.64 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
40
41
42
43
44
45
46
47
48
49
50
51
let playerScore = 0;
let computerScore = 0;
const choices = ['rock', 'paper', 'scissors'];
function getComputerChoice() {
const randomIndex = Math.floor(Math.random() * choices.length);
return choices[randomIndex];
}
function playRound(playerSelection) {
const computerSelection = getComputerChoice();
const resultText = document.getElementById('result-text');
if (playerSelection === computerSelection) {
resultText.textContent = `It's a tie! You both chose ${playerSelection}.`;
} else if (
(playerSelection === 'rock' && computerSelection === 'scissors') ||
(playerSelection === 'paper' && computerSelection === 'rock') ||
(playerSelection === 'scissors' && computerSelection === 'paper')
) {
playerScore++;
resultText.textContent = `You win! ${playerSelection} beats ${computerSelection}.`;
} else {
computerScore++;
resultText.textContent = `You lose! ${computerSelection} beats ${playerSelection}.`;
}
updateScore();
}
function updateScore() {
document.getElementById('player-score').textContent = playerScore;
document.getElementById('computer-score').textContent = computerScore;
}
function resetGame() {
playerScore = 0;
computerScore = 0;
document.getElementById('result-text').textContent = '';
updateScore();
}
document.querySelectorAll('.choice').forEach(button => {
button.addEventListener('click', () => {
const playerSelection = button.id;
playRound(playerSelection);
});
});
document.getElementById('reset').addEventListener('click', resetGame);