-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
176 lines (148 loc) · 6.29 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
document.addEventListener('DOMContentLoaded', function() {
// Variable Declarations
const winningScore = 5;
let playerScore = 0;
let computerScore = 0;
// DOM elements
let playerDisplayScore = document.querySelector('#playerScore');
let computerDisplayScore = document.querySelector('#computerScore');
const result = document.querySelector('#displayWinner');
const announceWinner = document.querySelector('#modalWinner');
const playAgainText = document.querySelector('#playAgain');
const showModal = document.querySelector('#myModal');
const playerImage = document.getElementById('playerImage');
const computerImage = document.getElementById('computerImage');
// Images
const rockImage = './images/1FAA8_color.png';
const paperImage = './images/1F9FB_color.png';
const scissorsImage = './images/2702_color.png';
//Functions
function getComputerChoice() {
const pick = ['rock', 'paper', 'scissors'];
return pick[Math.floor(Math.random() * pick.length)];
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function displayVS() {
const vsText = 'VS';
const vsElement = document.getElementById('displayTextVs');
vsElement.innerText = vsText;
return vsText;
}
function displayImage(playerSelection, computerSelection) {
const altTextComputer = 'Display computers choice';
const altTextPlayer = 'Display players choice';
switch (playerSelection.toLowerCase()) {
case 'rock':
playerImage.src = rockImage;
playerImage.alt = altTextPlayer;
break;
case 'paper':
playerImage.src = paperImage;
playerImage.alt = altTextPlayer;
break;
case 'scissors':
playerImage.src = scissorsImage;
playerImage.alt = altTextPlayer;
break;
}
switch (computerSelection) {
case 'rock':
computerImage.src = rockImage;
computerImage.alt = altTextComputer;
break;
case 'paper':
computerImage.src = paperImage;
computerImage.alt = altTextComputer;
break;
case 'scissors':
computerImage.src = scissorsImage;
computerImage.alt = altTextComputer;
break;
}
}
function displayGameResult(playerScore, computerScore, announceWinner) {
showModal.classList.add('showModal');
playAgainText.innerText = 'Want to play again? Click on the game controller.';
if (playerScore > computerScore) {
return announceWinner.innerText = `Congratulations! You won the game with a score of ${playerScore} to ${computerScore}!`;
} else if (playerScore < computerScore) {
return announceWinner.innerText = `You lost the game with a score of ${playerScore} to ${computerScore}!`;
} else {
return announceWinner.innerText = "It's a tie! Try harder next time.";
}
}
function playRound(playerSelection) {
const computerSelection = getComputerChoice();
if (playerSelection.toLowerCase() === computerSelection) {
displayVS();
displayImage(playerSelection, computerSelection);
return result.innerText = `It's a tie! You both picked ${playerSelection.toLowerCase()}.`;
}
else if (
(playerSelection.toLowerCase() === 'rock' && computerSelection === 'scissors') ||
(playerSelection.toLowerCase() === 'paper' && computerSelection === 'rock') ||
(playerSelection.toLowerCase() === 'scissors' && computerSelection === 'paper')
) {
playerScore++;
playerDisplayScore.innerText = `${playerScore}`;
displayVS();
displayImage(playerSelection, computerSelection);
return result.innerText = `You win! ${playerSelection} beats ${computerSelection}.`;
}
else {
computerScore++;
computerDisplayScore.innerText = `${computerScore}`;
displayVS();
displayImage(playerSelection, computerSelection);
let valueToUpperCase = capitalizeFirstLetter(computerSelection);
return result.innerText = `You loose! ${valueToUpperCase} beats ${playerSelection.toLowerCase()}.`;
}
}
function playGame() {
if (playerScore < winningScore && computerScore < winningScore) {
} else {
if (playerScore > computerScore) {
return displayGameResult(playerScore, computerScore, announceWinner);
} else if (playerScore < computerScore) {
return displayGameResult(playerScore, computerScore, announceWinner);
} else {
return displayGameResult(playerScore, computerScore, announceWinner);
}
}
}
//Event listeners
const btnRock = document.querySelector('#btnRock');
btnRock.addEventListener('click', () => {
const playerSelection= "Rock";
playRound(playerSelection);
playGame();
});
const btnPaper = document.querySelector('#btnPaper');
btnPaper.addEventListener('click', () => {
const playerSelection = 'Paper';
playRound(playerSelection);
playGame();
});
const btnScissor = document.querySelector('#btnScissor');
btnScissor.addEventListener('click', () => {
const playerSelection = 'Scissors';
playRound(playerSelection);
playGame();
});
const btnReset =document.querySelector('#restartGame');
btnReset.addEventListener ('click', () => {
playerScore = 0;
computerScore = 0;
playerDisplayScore.innerText = 0;
computerDisplayScore.innerText = 0;
playerImage.src = '';
playerImage.alt = '';
computerImage.src = '';
computerImage.alt = '';
result.innerText = '';
displayTextVs.innerText = '';
showModal.classList.remove('showModal');
})
});