-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperfectcubes.js
More file actions
250 lines (210 loc) · 7.65 KB
/
perfectcubes.js
File metadata and controls
250 lines (210 loc) · 7.65 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
// SET-UP
var numberCorrect = 0,
numberGuesses = 0,
hasIncreased = 0;
function resetScoreboard() {
numberCorrect = 0;
hasIncreased = 0;
numberGuesses = 0;
document.getElementById("correct").innerHTML = "0";
document.getElementById("guesses").innerHTML = "0";
document.getElementById("percent").innerHTML = "0";
}
function updateScore() {
document.getElementById("correct").innerHTML = numberCorrect;
document.getElementById("guesses").innerHTML = numberGuesses;
document.getElementById("percent").innerHTML = Math.floor((numberCorrect / numberGuesses) * 100) + "%";
}
var whichIncorrect = 0,
incorrectResponses = ["Not quite, but keep at it!", "I'm sorry, but give it another go!", "No, but don't give up!"];
function isCorrect(correct) {
if (hasIncreased == 0) {
numberGuesses++;
}
if (correct) {
numberCorrect++;
hasIncreased = 1;
document.getElementById("result").innerHTML = '<h2 style="color: green">Correct!</h2>';
document.getElementById("check").style.visibility = "hidden";
document.getElementById("nextProb").style.visibility = "visible";
} else {
document.getElementById("result").innerHTML =
'<h2 style="color: darkred">' + incorrectResponses[whichIncorrect] + "</h2>";
whichIncorrect++;
if (whichIncorrect == incorrectResponses.length) {
whichIncorrect = 0;
}
}
document.getElementById("result").style.visibility = "visible";
if (numberCorrect == 10) {
throwConfetti();
}
}
function throwConfetti() {
const fullscreenDiv = document.createElement("div");
fullscreenDiv.style.position = "fixed";
fullscreenDiv.style.top = 0;
fullscreenDiv.style.left = 0;
fullscreenDiv.style.width = "100%";
fullscreenDiv.style.height = "100%";
fullscreenDiv.style.backgroundColor = "transparent";
fullscreenDiv.style.zIndex = 9999;
const gifImage = document.createElement("img");
gifImage.src = "Images/confetti.gif";
gifImage.style.width = "100%";
gifImage.style.height = "100%";
gifImage.style.objectFit = "contain";
fullscreenDiv.appendChild(gifImage);
document.body.appendChild(fullscreenDiv);
const cleanUp = setTimeout(cleanUpConfetti, 2000, fullscreenDiv);
}
function cleanUpConfetti(thing) {
document.body.removeChild(thing);
}
// PAGE SPECIFIC
function startFresh() {
resetScoreboard();
clearResult();
nextProb();
}
function clearResult() {
document.getElementById("result").innerHTML = '<h2 style="color: green">--</h2>';
document.getElementById("result").style.visibility = "hidden";
hasIncreased = 0;
document.getElementById("check").style.visibility = "visible";
document.getElementById("nextProb").style.visibility = "hidden";
document.getElementById("startFresh").innerHTML = "Start Fresh";
var current;
for (let i = 1; i < 11; i++) {
current = document.getElementById("cb" + i);
current.style.backgroundColor = "white";
current.value = "";
}
document.getElementById("cbTable").style.visibility = "visible";
MathJax.typeset();
}
function debug() {
var temp = "Stuff here\n";
alert(temp);
}
function nextProb() {
// Build the problem here
clearResult();
}
function check() {
var allCorrect = true, current;
// check that inputs match the answers
for (let i = 1; i < 11; i++) {
current = document.getElementById("cb" + i);
if (current.value == current.dataset.solution) {
current.style.backgroundColor = "lightgreen";
} else {
current.style.backgroundColor = "lightcoral";
allCorrect = false;
}
}
if (allCorrect) {
isCorrect(true);
} else {
isCorrect(false);
}
updateScore();
}
// SET-UP RANDOM PERFECT SQUARES
var numberCorrect2 = 0,
numberGuesses2 = 0,
hasIncreased2 = 0;
function resetScoreboard2() {
numberCorrect2 = 0;
hasIncreased2 = 0;
numberGuesses2 = 0;
document.getElementById("correct2").innerHTML = "0";
document.getElementById("guesses2").innerHTML = "0";
document.getElementById("percent2").innerHTML = "0";
}
function updateScore2() {
document.getElementById("correct2").innerHTML = numberCorrect2;
document.getElementById("guesses2").innerHTML = numberGuesses2;
document.getElementById("percent2").innerHTML = Math.floor((numberCorrect2 / numberGuesses2) * 100) + "%";
}
var whichIncorrect2 = 0,
incorrectResponses2 = ["Not quite, but keep at it!", "I'm sorry, but give it another go!", "No, but don't give up!"];
function isCorrect2(correct) {
if (hasIncreased2 == 0) {
numberGuesses2++;
}
if (correct) {
numberCorrect2++;
hasIncreased2 = 1;
document.getElementById("result2").innerHTML = '<h2 style="color: green">Correct!</h2>';
document.getElementById("check2").style.visibility = "hidden";
document.getElementById("nextProb2").style.visibility = "visible";
} else {
document.getElementById("result2").innerHTML =
'<h2 style="color: darkred">' + incorrectResponses2[whichIncorrect2] + "</h2>";
whichIncorrect2++;
if (whichIncorrect2 == incorrectResponses2.length) {
whichIncorrect2 = 0;
}
}
document.getElementById("result2").style.visibility = "visible";
if (numberCorrect2 == 14) {
throwConfetti();
}
}
function clearResult2() {
document.getElementById("result2").innerHTML = '<h2 style="color: green">--</h2>';
document.getElementById("result2").style.visibility = "hidden";
hasIncreased2 = 0;
document.getElementById("check2").style.visibility = "visible";
document.getElementById("nextProb2").style.visibility = "hidden";
document.getElementById("startFresh2").innerHTML = "Start Fresh";
}
function throwConfetti2() {
const fullscreenDiv = document.createElement("div");
fullscreenDiv.style.position = "fixed";
fullscreenDiv.style.top = 0;
fullscreenDiv.style.left = 0;
fullscreenDiv.style.width = "100%";
fullscreenDiv.style.height = "100%";
fullscreenDiv.style.backgroundColor = "transparent";
fullscreenDiv.style.zIndex = 9999;
const gifImage = document.createElement("img");
gifImage.src = "Images/confetti.gif";
gifImage.style.width = "100%";
gifImage.style.height = "100%";
gifImage.style.objectFit = "contain";
fullscreenDiv.appendChild(gifImage);
document.body.appendChild(fullscreenDiv);
const cleanUp = setTimeout(cleanUpConfetti2, 2000, fullscreenDiv);
}
function cleanUpConfetti2(thing) {
document.body.removeChild(thing);
}
function startFresh2() {
resetScoreboard2();
nextProb2();
}
var initialValue = 1, perfCB = 1;
function nextProb2() {
initialValue = Math.floor(Math.random()*10) + 1;
perfCB = initialValue * initialValue * initialValue;
document.getElementById("answerInput2Label").innerHTML = String.raw`\(` + initialValue + String.raw`^3=\)`;
clearResult2();
resetInputs2();
}
function resetInputs2() {
// Display the build function and reset the inputs
document.getElementById("answerInput2").value = "";
MathJax.typeset();
// get inputs by ID and set .value to 0 or ""
}
function check2() {
var allCorrect2 = true;
if (document.getElementById("answerInput2").value == perfCB) {
isCorrect2(true);
} else {
isCorrect2(false);
}
updateScore2();
}