-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
98 lines (77 loc) · 3 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
function computerPlay() {
const choices = ['rock', 'paper', 'scissors'];
const random = Math.random();
let randomChoice = random * choices.length;
randomChoice = Math.floor(randomChoice);
return choices[randomChoice];
}
const playerChoice = document.querySelectorAll('.playerChoice');
playerChoice.forEach((currentBtn) => {
currentBtn.addEventListener('click', choiceHandler);
})
function isLastRound(){
const neededWins = 5;
const isLastRound = playerScore === neededWins || computerScore === neededWins;
return isLastRound;
}
let computerScore = 0;
let playerScore = 0;
function playRound(playerSelection, computerSelection) {
let result = 'You won!';
// check who scores a point
if (playerSelection == computerSelection) {
result = 'You are Tied';
} else if (playerSelection == 'rock' && computerSelection == 'paper'
|| playerSelection == 'scissors' && computerSelection == 'rock'
|| playerSelection == 'paper' && computerSelection == 'scissors') {
result = 'You lost';
}
// keep track of the score
if (result === 'You lost') {
computerScore++;
} else if (result === 'You won!') {
playerScore++;
}
return result;
}
function endRoundUIUpdate(roundResult, playerSelection, computerSelection){
const gameMsg = document.querySelector('#gameMsg');
gameMsg.textContent = `Your choice: ${playerSelection} VS computer's choice: ${computerSelection}.`;
// keep track of the score
if (roundResult !== 'You lost' && roundResult !== 'You won!') {
gameMsg.textContent = `Oh no, you are tied! Try again.`;
}
// check who won the game
if (isLastRound()) {
if (playerScore > computerScore) {
gameMsg.textContent = `You won!`;
} else {
gameMsg.textContent = 'The computer won!';
}
}
const playerResult = document.querySelector('#playerResult');
playerResult.textContent = `Player score: ${playerScore}`;
const computerResult = document.querySelector('#computerResult');
computerResult.textContent = `Computer score: ${computerScore}`;
}
function choiceHandler(event){
const clickedButton = event.currentTarget
const playerSelection = clickedButton.textContent.toLowerCase();
const computerSelection = computerPlay();
if (isLastRound()) {
return;
}
let roundResult = playRound(playerSelection, computerSelection);
endRoundUIUpdate(roundResult, playerSelection, computerSelection);
}
const resetBtn = document.querySelector('.resetBtn');
resetBtn.addEventListener('click', () => {
playerScore = 0;
computerScore = 0;
const playerResult = document.querySelector('#playerResult');
playerResult.textContent = `Player score: 0`;
const computerResult = document.querySelector('#computerResult');
computerResult.textContent = `Computer score: 0`;
const gameMsg = document.querySelector('#gameMsg');
gameMsg.textContent = 'New game started';
});