-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (47 loc) · 1.85 KB
/
Copy pathscript.js
File metadata and controls
55 lines (47 loc) · 1.85 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* Global Variables */
let computerScore = 0;
let playerScore = 0;
let round = 0;
/* Query Selectors */
const playerChoice = document.querySelectorAll('button.rps-buttons');
const playerScoreSection = document.querySelector('#player-score');
const computerScoreSection = document.querySelector('#computer-score');
const roundWinnerSection = document.querySelector('#winner');
const roundNumberSection = document.querySelector('#round-number');
const announceWinnerSection = document.querySelector('#announce-winner');
/* Functions */
function computerPlay(){
const availableChoices = ["rock", "paper", "scissors"];
const computerChoice = availableChoices[Math.floor(Math.random() * availableChoices.length)];
return computerChoice;
}
function updateRound(){
round++;
roundNumberSection.textContent = `Round ${round}`;
}
function updateScore(){
playerScoreSection.textContent = playerScore;
computerScoreSection.textContent = computerScore;
}
function playRound(computerSelection, playerSelection){
updateRound();
if (playerSelection === computerSelection){
roundWinnerSection.textContent = `${playerSelection} vs ${computerSelection}: Tie Game`;
return;
} else if (playerSelection === "rock" && computerSelection === "scissors" ||
playerSelection === "paper" && computerSelection === "rock" ||
playerSelection === "scissors" && computerSelection === "paper"){
roundWinnerSection.textContent = `${playerSelection} vs ${computerSelection}: Player Wins`;
playerScore++;
} else {
roundWinnerSection.textContent = `${playerSelection} vs ${computerSelection}: Computer Wins`;
computerScore++;
}
updateScore();
}
/* Event listeners */
playerChoice.forEach(element => {
element.addEventListener('click', () => {
playRound(computerPlay(), element.id);
});
})