Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
250 lines (223 loc) · 7.08 KB
/
Copy pathscript.js
File metadata and controls
250 lines (223 loc) · 7.08 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const gamePage = document.getElementById("game-page");
const scorePage = document.getElementById("score-page");
const splashPage = document.getElementById("splash-page");
const countdownPage = document.getElementById("countdown-page");
const startForm = document.getElementById("start-form");
const radioContainers = document.querySelectorAll(".radio-container");
const radioInputs = document.querySelectorAll("input");
const bestScores = document.querySelectorAll(".best-score-value");
const countdown = document.querySelector(".countdown");
const itemContainer = document.querySelector(".item-container");
const finalTimeEl = document.querySelector(".final-time");
const baseTimeEl = document.querySelector(".base-time");
const penaltyTimeEl = document.querySelector(".penalty-time");
const playAgainBtn = document.querySelector(".play-again");
const socialButtons = document.querySelector(".social-buttons");
let questionAmount = 0;
let equationsArray = [];
let playerGuessArray = [];
let bestScoreArray = [];
let firstNumber = 0;
let secondNumber = 0;
let equationObject = {};
const wrongFormat = [];
let timer;
let timePlayed = 0;
let baseTime = 0;
let penaltyTime = 0;
let finalTime = 0;
let finalTimeDisplay = "0.0";
let valueY = 0;
function bestScoresToDOM() {
bestScores.forEach((bestScore, index) => {
const bestScoreEl = bestScore;
bestScoreEl.textContent = `${bestScoreArray[index].bestScore}s`;
});
}
function getSavedBestScores() {
if (localStorage.getItem("bestScores")) {
bestScoreArray = JSON.parse(localStorage.bestScores);
} else {
bestScoreArray = [
{ questions: 10, bestScore: finalTimeDisplay },
{ questions: 25, bestScore: finalTimeDisplay },
{ questions: 50, bestScore: finalTimeDisplay },
{ questions: 99, bestScore: finalTimeDisplay },
];
localStorage.setItem("bestScores", JSON.stringify(bestScoreArray));
}
bestScoresToDOM();
}
function updateBestScore() {
bestScoreArray.forEach((score, index) => {
if (questionAmount == score.questions) {
const savedBestScore = Number(bestScoreArray[index].bestScore);
if (savedBestScore === 0 || savedBestScore > finalTime) {
bestScoreArray[index].bestScore = finalTimeDisplay;
}
}
});
bestScoresToDOM();
localStorage.setItem("bestScores", JSON.stringify(bestScoreArray));
}
function playAgain() {
gamePage.addEventListener("click", startTimer);
scorePage.hidden = true;
splashPage.hidden = false;
equationsArray = [];
playerGuessArray = [];
valueY = 0;
playAgainBtn.hidden = true;
socialButtons.style.display = "flex";
}
function showScorePage() {
socialButtons.style.display = "none";
setTimeout(() => {
playAgainBtn.hidden = false;
}, 1000);
gamePage.hidden = true;
scorePage.hidden = false;
}
function scoresToDOM() {
finalTimeDisplay = finalTime.toFixed(1);
baseTime = timePlayed.toFixed(1);
penaltyTime = penaltyTime.toFixed(1);
baseTimeEl.textContent = `Base Time: ${baseTime}s`;
penaltyTimeEl.textContent = `Penalty: +${penaltyTime}s`;
finalTimeEl.textContent = `${finalTimeDisplay}s`;
updateBestScore();
itemContainer.scrollTo({ top: 0, behavior: "instant" });
showScorePage();
}
function checkTime() {
if (playerGuessArray.length == questionAmount) {
clearInterval(timer);
equationsArray.forEach((equation, index) => {
if (equation.evaluated !== playerGuessArray[index]) {
penaltyTime += 0.5;
}
});
finalTime = timePlayed + penaltyTime;
scoresToDOM();
}
}
function addTime() {
timePlayed += 0.1;
checkTime();
}
function startTimer() {
timePlayed = 0;
penaltyTime = 0;
finalTime = 0;
timer = setInterval(addTime, 100);
gamePage.removeEventListener("click", startTimer);
}
function select(guessedTrue) {
valueY += 80;
itemContainer.scroll(0, valueY);
return guessedTrue
? playerGuessArray.push("true")
: playerGuessArray.push("false");
}
function showGamePage() {
gamePage.hidden = false;
countdownPage.hidden = true;
}
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function createEquations() {
const correctEquations = getRandomInt(questionAmount);
const wrongEquations = questionAmount - correctEquations;
for (let i = 0; i < correctEquations; i++) {
firstNumber = getRandomInt(9);
secondNumber = getRandomInt(9);
const equationValue = firstNumber * secondNumber;
const equation = `${firstNumber} x ${secondNumber} = ${equationValue}`;
equationObject = { value: equation, evaluated: "true" };
equationsArray.push(equationObject);
}
for (let i = 0; i < wrongEquations; i++) {
firstNumber = getRandomInt(9);
secondNumber = getRandomInt(9);
const equationValue = firstNumber * secondNumber;
wrongFormat[0] = `${firstNumber} x ${secondNumber + 1} = ${equationValue}`;
wrongFormat[1] = `${firstNumber} x ${secondNumber} = ${equationValue - 1}`;
wrongFormat[2] = `${firstNumber + 1} x ${secondNumber} = ${equationValue}`;
const formatChoice = getRandomInt(2);
const equation = wrongFormat[formatChoice];
equationObject = { value: equation, evaluated: "false" };
equationsArray.push(equationObject);
}
shuffle(equationsArray);
}
function equationsToDOM() {
equationsArray.forEach((equation) => {
const item = document.createElement("div");
item.classList.add("item");
const equationText = document.createElement("h1");
equationText.textContent = equation.value;
item.appendChild(equationText);
itemContainer.appendChild(item);
});
}
function populateGamePage() {
itemContainer.textContent = "";
const topSpacer = document.createElement("div");
topSpacer.classList.add("height-240");
const selectedItem = document.createElement("div");
selectedItem.classList.add("selected-item");
itemContainer.append(topSpacer, selectedItem);
createEquations();
equationsToDOM();
const bottomSpacer = document.createElement("div");
bottomSpacer.classList.add("height-500");
itemContainer.appendChild(bottomSpacer);
}
function countdownStart() {
countdown.textContent = "3";
setTimeout(() => {
countdown.textContent = "2";
}, 1000);
setTimeout(() => {
countdown.textContent = "1";
}, 2000);
setTimeout(() => {
countdown.textContent = "GO!";
}, 3000);
}
function showCountdown() {
socialButtons.style.display = "none";
countdownPage.hidden = false;
splashPage.hidden = true;
countdownStart();
populateGamePage();
setTimeout(showGamePage, 4000);
}
function getRadioValue() {
let radioValue;
radioInputs.forEach((radioInput) => {
if (radioInput.checked) {
radioValue = radioInput.value;
}
});
return radioValue;
}
function selectQuestionAmount(e) {
e.preventDefault();
questionAmount = getRadioValue();
if (questionAmount) {
showCountdown();
}
}
startForm.addEventListener("click", () => {
radioContainers.forEach((radioEl) => {
radioEl.classList.remove("selected-label");
if (radioEl.children[1].checked) {
radioEl.classList.add("selected-label");
}
});
});
gamePage.addEventListener("click", startTimer);
startForm.addEventListener("submit", selectQuestionAmount);
getSavedBestScores();