-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
301 lines (220 loc) · 8.11 KB
/
Copy pathscript.js
File metadata and controls
301 lines (220 loc) · 8.11 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//CORE MATH
let lastAnswer = 0; // Variable to store the last answer
let problemsSolved = 0; // Variable to keep track of the number of problems solved
function generateProblem() {
let num1 = lastAnswer; // Start with the last answer
let num2 = Math.floor(Math.random() * 100);
let operation;
let rampDown = 0.5;
const absSize = Math.abs(lastAnswer);
if (absSize >= 100) {
rampDown = 0.5 - (absSize / 400)
if (num2 < 50) {
num2 += absSize / 10
}
}
num2 = Math.round(num2);
// Adjust the probability of choosing addition or subtraction based on lastAnswer
if (lastAnswer < -100) {
// Increase probability of addition
operation = Math.random() > rampDown ? '+' : '-'; // (1.0 - rampDown) chance of addition
} else if (lastAnswer > 100) {
// Increase probability of subtraction
operation = Math.random() > rampDown ? '-' : '+'; // (1.0 - rampDown) chance of subtraction
} else {
// If lastAnswer is between -100 and 100, choose randomly
operation = Math.random() > 0.5 ? '+' : '-';
}
let answer;
if (operation === '+') {
answer = num1 + num2;
} else {
answer = num1 - num2;
}
return { question: `${num1} ${operation} ${num2}`, answer: answer };
}
function checkAnswer() {
const userAnswer = parseInt(document.getElementById('userAnswer').value);
const problem = document.currentProblem;
if (userAnswer === problem.answer) {
lastAnswer = problem.answer; // Update the last answer
problemsSolved++; // Increment the problems solved counter
updateCounter(); // Update the display of the counter
setTimeout(loadNewProblem, 150); // Delay
}
}
//
// TIMER
const startTime = 60000; // 1 minute = 60,000 hundredths of a second
let timeLeft = startTime;
const start = 1;
let timerWorker;
function startTimer() {
if (window.Worker) {
timerWorker = new Worker('timerWorker2.js');
timerWorker.postMessage([1, startTime]); // 1 = start timerWorker
timerWorker.onmessage = function (e) {
let timeLeft = e.data;
// Convert milliseconds to seconds (1 second = 1000 milliseconds)
let seconds = Math.floor(timeLeft / 1000);
// Calculate hundredths of a second
// (e.g., 1250 milliseconds is 1 second and 250 milliseconds or 25 hundredths)
let hundredths = Math.floor((timeLeft % 1000) / 10);
hundredths = hundredths < 10 ? '0' + hundredths : hundredths;
document.getElementById('timer').textContent = seconds + '.' + hundredths;
if (timeLeft <= 0) {
timerWorker.terminate(); // Stop the worker when the timer ends
showResultsPopup();
}
};
}
}
//
//CORE HELPERS
function resetAll() {
problemsSolved = 0;
lastAnswer = 0;
timeLeft = 6000;
loadNewProblem();
}
//Score for results popup
function updateScore() {
var scoreElement = document.getElementById("score");
scoreElement.innerHTML = "Score: " + problemsSolved;
}
//Counter for play screen
function updateCounter() {
document.getElementById('counter').textContent = `${problemsSolved}`;
}
function loadNewProblem() {
const problem = generateProblem();
document.currentProblem = problem;
document.getElementById('problem').innerHTML = `<span class="num1-color">${problem.question.split(' ')[0]}</span> ${problem.question.substring(problem.question.indexOf(' '))} =`;
document.getElementById('userAnswer').value = '';
updateCounter(); // Ensure the counter is updated when the new problem is loaded
}
//
//CORE RUN OPERATIONS
function showResultsPopup() {
document.getElementById('resultsPopup').style.display = 'block';
document.getElementById('problemContainer').style.display = 'none'; // Hide the problem container
document.getElementById('keyboard').style.display = 'none';
document.getElementById('counter').style.display = 'none';
document.getElementById('timer').style.display = 'none';
document.getElementById('top-bar').style.display = 'none';
document.getElementById('my-canvas').style.display = 'block';
updateScore();
startConfetti();
}
function playAgain() {
document.getElementById('my-canvas').style.display = 'none';
document.getElementById('resultsPopup').style.display = 'none';
document.getElementById('problemContainer').style.display = 'flex'; // Show the problem container again
document.getElementById('counter').style.display = 'flex';
document.getElementById('timer').style.display = 'flex';
document.getElementById('top-bar').style.display = 'block';
keyboardShowCheck();
resetAll();
startTimer();
document.getElementById('userAnswer').focus(); // Set focus to the input box
stopConfetti();
}
window.onload = function () {
setAllFontSize();
keyboardShowCheck();
loadNewProblem();
document.getElementById('userAnswer').focus(); // Focus the invisible input box for user input
document.getElementById('userAnswer').addEventListener('input', checkAnswer);
startTimer(); // Start the timer when the window loads
};
//
//CONFETTI
//Dynamic speed (bigger height = more space to fall = faster speed)
var confettiSettings = { target: 'my-canvas', start_from_edge: 'true', max: '100', clock: window.innerHeight/10 };
var confetti = new ConfettiGenerator(confettiSettings);
function startConfetti() {
confetti = new ConfettiGenerator(confettiSettings);
confetti.render();
}
function stopConfetti() {
confetti.clear();
}
//
//KEYBOARD
function enterNumber(number) {
if (document.getElementById('userAnswer').value.length<5){
document.getElementById('userAnswer').value += number;
checkAnswer();
}
}
function backspace() {
var userAnswer = document.getElementById('userAnswer');
userAnswer.value = userAnswer.value.slice(0, -1); // Remove last character
}
function negative() {
var userAnswerElement = document.getElementById('userAnswer');
var userAnswer = userAnswerElement.value;
if (userAnswer === '' || userAnswer === '-') {
// Toggle between empty and '-'
userAnswerElement.value = userAnswer === '' ? '-' : '';
} else {
// If userAnswer contains a number, toggle its sign
var numericValue = parseFloat(userAnswer);
if (!isNaN(numericValue)) {
userAnswerElement.value = -numericValue;
}
}
}
function keyboardShowCheck() {
var isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
var isPortrait = window.innerHeight > window.innerWidth;
if (!isMobile || ! isPortrait) {
document.getElementById('keyboard').style.display = 'none';
}
else {
document.getElementById('keyboard').style.display = 'grid';
}
}
//
//RESPONSIVE FONTS
function getSmallerVWVH(percent) {
// Get viewport width and height
let vw = window.innerWidth;
let vh = window.innerHeight;
// Calculate 10% of viewport width and height
let usedVW = vw * percent / 100;
let usedVH = vh * percent / 100;
// Return the smaller of the two values
return Math.round(Math.min(usedVW, usedVH));
}
// Modular function to set font size
function setFontSize(selector, percent) {
let elements;
// Determine the type of the selector and get the elements
if (selector.startsWith('#')) {
// ID selector
elements = [document.getElementById(selector.substring(1))];
} else if (selector.startsWith('.')) {
// Class selector
elements = document.getElementsByClassName(selector.substring(1));
} else {
// Tag selector
elements = document.getElementsByTagName(selector);
}
// Set font size for the selected elements
for (let element of elements) {
if (element) {
element.style.fontSize = getSmallerVWVH(percent) + 'px';
}
}
}
function setAllFontSize() {
setFontSize('#userAnswer', 10);
setFontSize('#problem', 10);
setFontSize('h1', 20);
setFontSize('.popup-content', 10);
}
window.onresize = function () {
setAllFontSize();
};
//