-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.js
266 lines (235 loc) · 7.82 KB
/
math.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
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
"use strict"
const socket=io();
socket.on("userJoined",(id)=>console.log("user joined same room with id :"+id))
socket.on('scoreUpdateToClient',(id,scr)=>console.log(id,scr))
var from1
var to1
var from2
var to2
let questionLeft
let operator
let correct = 0;
let incorrect = 0;
let num1;
let num2;
let ans;
let userAns;
let intervalId;
formRead();
document.getElementById("userAns").focus()
function formRead() {
from1 = +document.forms['settings']['from1'].value;
to1 = +document.forms["settings"]["to1"].value;
from2 = +document.forms["settings"]["from2"].value;
to2 = +document.forms["settings"]["to2"].value;
questionLeft = +document.forms["settings"]["NumberOfQuestions"].value;
operator = [];
operatorsSelected();
correct = 0;
incorrect = 0;
document.getElementById("correct").innerHTML = "correct : " + correct;
document.getElementById("incorrect").innerHTML = "incorrect : " + incorrect
showQues()
}
function quesmul() {
num1 = intRange(from1, to1);
num2 = intRange(from2, to2);
ans = (num1) * (num2);
let ques = `${num1}×${num2}`
document.getElementById("question").innerHTML = ques;
document.getElementById("userAns").value = ""
}
function quesadd() {
num1 = intRange(from1, to1);
num2 = intRange(from2, to2);
ans = (num1) + (num2);
let ques = `${num1}+${num2}`
document.getElementById("question").innerHTML = ques;
document.getElementById("userAns").value = ""
}
function quessub() {
num1 = intRange(from1, to1);
num2 = intRange(from2, to2);
ans = (num1) - (num2);
let ques = `${num1}-${num2}`
document.getElementById("question").innerHTML = ques;
document.getElementById("userAns").value = "";
}
function quesdiv() {
num1 = intRange(from1, to1);
num2 = intRange(from2, to2);
ans = (num1) / (num2);
let ques = `${num1}/${num2}`
document.getElementById("question").innerHTML = ques;
document.getElementById("userAns").value = ""
}
function autoEnter() {
if (Math.abs(+document.getElementById("userAns").value - ans) < .01) {
check()
}
} //auto enters the answer if it's corrrect
let timeElapsed=0
function check() {
if(correct==0&&incorrect==0){
let interval=1;//in seconds
timerStart();
function timerStart() {
intervalId= setInterval(() => {
timeElapsed+=interval;
let min=Math.floor(timeElapsed/60);
let sec=timeElapsed%60;
document.getElementById("timer").innerHTML=`${min} : ${sec}`
}, 1000*interval);
}
}
userAns = +document.getElementById("userAns").value
if (Math.abs(ans - userAns) < .01) {
correct++;
document.getElementById("correct").innerHTML = "correct : " + correct;
} else {
incorrect++;
document.getElementById("incorrect").innerHTML = "incorrect : " + incorrect
};
if (--questionLeft > 0) {
document.getElementById("notice").innerHTML = `${questionLeft} Question Remaining`
showQues()
} else {
clearInterval(intervalId);
document.getElementById("notice").innerHTML = "Well Done ! Refresh page or Modify settings below to continue practicing.";
attentionGet("notice", 4);
}
// let score=timeElapsed?(correct-incorrect/4)/timeElapsed:0;
score=correct;
console.log(score)
socket.emit('scoreUpdate',score)
}
function showQues() {
if (document.getElementById("real").checked) {
quesReal()
} else {
randomElement(operator)()
};
}
function operatorsSelected() {
if (document.getElementById("add").checked) {
operator.push(quesadd)
};
if (document.getElementById("sub").checked) {
operator.push(quessub)
};
if (document.getElementById("div").checked) {
operator.push(quesdiv)
};
if (document.getElementById("mul").checked || operator.length == 0) { //if no operator is selected push mul.
operator.push(quesmul)
};
}
function attentionGet(id, strength = 1, color = "Yellow") {
document.getElementById(id).style.backgroundColor = color;
setTimeout(() => {
document.getElementById(id).style.backgroundColor = ""
}, 500 * strength);
}
function intRange(a, b) {
return Math.floor(Math.random() * (b - a)) + a;
} //random int from a to b-1
function randomElement(inputArray) {
let randomIndex = intRange(0, inputArray.length)
return inputArray[randomIndex]
}
function quesReal() {
if (ans < 500) {
mulreal()
} else if (ans < 1000) {
addreal()
} else {
subreal()
};
function mulreal() {
num1 = userAns || intRange(from1, to1);
num2 = intRange(0, 20);
ans = (num1) * (num2);
let ques = `${num1}×${num2}`
document.getElementById("question").innerHTML = ques;
document.getElementById("userAns").value = ""
};
function addreal() {
num1 = userAns || intRange(0, 1000);
num2 = intRange(from2, to2);
ans = (num1) + (num2);
let ques = `${num1}+${num2}`
document.getElementById("question").innerHTML = ques;
document.getElementById("userAns").value = ""
}
function subreal() {
num1 = userAns || intRange(from1, to1);
num2 = intRange(0, 1000);
ans = (num1) - (num2);
let ques = `${num1}-${num2}`
document.getElementById("question").innerHTML = ques;
document.getElementById("userAns").value = "";
}
}
function dropdownState(idToggled, idButton) {
if (document.getElementById(idToggled).style.display == "none") {
writeOn(idButton, '▼')
} else {
writeOn(idButton, '▲')
}
}
function hide(id) {
document.getElementById(id).style.display = "none"
};
function show(id) {
document.getElementById(id).style.display = "block"
};
function toggleVisibility(id) {
if (document.getElementById(id).style.display == "none") {
show(id)
} else hide(id)
}
function writeOn(id, message) {
document.getElementById(id).innerHTML = message
}
function toggleLevel() {
toggleVisibility('levelSet');
dropdownState('levelSet', 'levelDropdownSymbol')
}
//code related to service workers(for making app pwa) is commented out because it was not working
// if ('serviceWorker' in navigator) {
// window.addEventListener('load', function () {
// navigator.serviceWorker.register('/Math-Practice/sw.js').then(function (registration) {
// // Registration was successful
// console.log('ServiceWorker registration successful with scope: ', registration.scope);
// }, function (err) {
// // registration failed :(
// console.log('ServiceWorker registration failed: ', err);
// });
// });
// }
// let deferredPrompt;
// const addBtn = document.querySelector('.add-button');
// addBtn.style.display = 'none';
// window.addEventListener('beforeinstallprompt', (e) => {
// // Prevent Chrome 67 and earlier from automatically showing the prompt
// e.preventDefault();
// // Stash the event so it can be triggered later.
// deferredPrompt = e;
// // Update UI to notify the user they can add to home screen
// addBtn.style.display = 'block';
// addBtn.addEventListener('click', (e) => {
// // hide our user interface that shows our A2HS button
// addBtn.style.display = 'none';
// // Show the prompt
// deferredPrompt.prompt();
// // Wait for the user to respond to the prompt
// deferredPrompt.userChoice.then((choiceResult) => {
// if (choiceResult.outcome === 'accepted') {
// console.log('User accepted the A2HS prompt');
// } else {
// console.log('User dismissed the A2HS prompt');
// }
// deferredPrompt = null;
// });
// });
// });