-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
32 lines (28 loc) · 1.2 KB
/
index.js
File metadata and controls
32 lines (28 loc) · 1.2 KB
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
const choices = ['rock', 'paper', 'scissors'];
const playerScore = document.getElementById('playerScore');
const computerScore = document.getElementById('computerScore');
const playerDisplay = document.getElementById('playerDisplay');
const computerDisplay = document.getElementById('computerDisplay');
const resultDisplay = document.getElementById('resultDisplay');
function play(playerChoice) {
const computerMove = choices[Math.floor(Math.random() * choices.length)];
let result = '';
if (playerChoice === computerMove) {
result = "It's a Tie!";
}
else if (
(playerChoice === 'rock' && computerMove === 'scissors') ||
(playerChoice === 'paper' && computerMove === 'rock') ||
(playerChoice === 'scissors' && computerMove === 'paper')
) {
result = 'Player Wins!';
playerScore.textContent = Number(playerScore.textContent) + 1;
}
else {
result = 'Computer Wins!';
computerScore.textContent = Number(computerScore.textContent) + 1;
}
playerDisplay.textContent = `Player Move: ${playerChoice}`;
computerDisplay.textContent = `Computer Move: ${computerMove}`;
resultDisplay.textContent = `Result: ${result}`;
}