-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.js
279 lines (249 loc) · 7.72 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
267
268
269
270
271
272
273
274
275
276
277
278
279
var number = [0,1,2];
var boxContent = [0,1,2]; // 1 is first number, 2 is second number, 0 is result
var upTo = [0,10,10];
var guess = 0;
var operation = 'x';
var maxSeconds = 0;
var maxOperations = 0;
var GLOBAL_removeZeroes = 0;
var counterOK = 0;
var counterKO = 0;
var resultTable;
var GLOBAL_finished = 0;
var secondsInterval = 0;
var LAPSE_BETWEEN_OPERATIONS = 400;
initialize();
function initialize() {
// Recover parameters
upTo[1] = getParameterByName('firstNumber',10);
upTo[2] = getParameterByName('secondNumber',10);
var operationCode = getParameterByName('operation','MULT');
if (operationCode=='MULT') {
operation = 'x'
} else {
operation = '+'
}
maxSeconds = getParameterByName('maxSeconds',0);
maxOperations = getParameterByName('maxOperations',0);
GLOBAL_removeZeroes = getParameterByName('removeZeroes',0);
// Initialize counters
counterOK = 0;
counterKO = 0;
remainingSeconds = maxSeconds;
GLOBAL_remainingOperations = maxOperations;
// UI initialization
writeById('operation', operation);
writeById('counter-operations', GLOBAL_remainingOperations);
updateTime(remainingSeconds);
document.getElementById('play-area').className = '';
generateResultTable(upTo[1],upTo[2]);
var supportsTouch = ("ontouchstart" in window) || window.navigator.msMaxTouchPoints > 0;
if (!supportsTouch) {
document.querySelector('.keyboard').className = 'hidden';
}
// set events, timeouts, intervals...
addEvent(window, "keyup", eventHub);
document.querySelectorAll('.keyboard button').forEach(element => {
addEvent(element, "click", eventHub);
});
if (maxSeconds>0) {
secondsInterval = setInterval(function() {oneSecondPassed()}, 1000);
}
// Go on
newOperation();
}
function oneSecondPassed() {
remainingSeconds--;
updateTime(remainingSeconds);
if (remainingSeconds==0) {
testFinished();
}
}
function updateTime(seconds) {
var minutesAndSeconds = Math.floor(remainingSeconds/60) + 'm' + (remainingSeconds%60) + 's';
writeById('counter-time', minutesAndSeconds);
}
function testFinished() {
// Disable intervals, key events, etc
GLOBAL_finished = 1;
if (secondsInterval) {
clearInterval(secondsInterval);
}
document.getElementById('chalkboard').className='hidden'; //hide
document.getElementById('interface').className='hidden'; //hide
document.getElementById('results').className='results'; //remove hidden attribute
// Add result analysis
var resultsDescription = document.getElementById('result-description');
resultsDescription.innerHTML+=
'Aciertos:' + counterOK + '<br/>' +
'Errores:' + counterKO + '<br/>';
}
function newOperation() {
var minValue = 0;
if (GLOBAL_removeZeroes) {
minValue = 1;
}
number[1] = getRandomInt(minValue,upTo[1]);
number[2] = getRandomInt(minValue,upTo[2]);
var gotBackOnce = 0;
// ONLY use this number if it has not been asked yet
while (resultTable[number[1]][number[2]]!=0) {
if (number[1]<upTo[1]) {
number[1]++;
} else if (number[2]<upTo[2]) {
number[1]=0;
number[2]++;
} else {
if (gotBackOnce==1) {
testFinished();
return;
} else {
number[1]=0;
number[2]=0;
gotBackOnce = 1;
}
}
}
switch(operation) {
case '+': number[0] = number[1] + number[2];
break;
case 'x': number[0] = number[1] * number[2];
break;
}
guess = 0;
for (var i=0;i<3;i++) {
boxContent[i] = number[i];
}
elementToGuess = getRandomInt(0,2);
boxContent[elementToGuess]='?';
for (var i=0;i<=2;i++) {
showNumberContent(i,boxContent[i]);
}
}
function processDigit(digit) {
guess = guess * 10 + digit;
showNumberContent(elementToGuess,guess);
// Same number of digits?
var bypassZero = (operation=='x' && number[0]==0 && elementToGuess!=0);
if ((guess.toString().length == number[elementToGuess].toString().length) || bypassZero){
processResult(guess==number[elementToGuess] || bypassZero );
completedOperation();
}
}
function clearGuess() {
guess = 0;
showNumberContent(elementToGuess,'?');
}
function completedOperation () {
GLOBAL_remainingOperations--;
writeById('counter-operations', GLOBAL_remainingOperations);
if (GLOBAL_remainingOperations==0) {
testFinished()
} else {
setTimeout('newOperation();',LAPSE_BETWEEN_OPERATIONS); // Leave a little time, just for visual feedback
}
}
function animateCounter(counterId) {
var elem = document.getElementById(counterId);
var originalClassname = elem.className.replace(' animation-counter-increase','');
elem.className = originalClassname;
setTimeout(function () {
elem.className = originalClassname + ' animation-counter-increase';
}, 100);
}
function processResult(result) {
var resultCellKey = 'result-' + number[1] + '-' + number[2];
if (result) {
document.getElementById(resultCellKey).className = 'cell-ok';
resultTable[number[1]][number[2]] = 1;
counterOK = counterOK + 1;
animateCounter('img-counter-ok');
writeById('counter-ok',counterOK);
} else {
document.getElementById(resultCellKey).className = 'cell-ko';
resultTable[number[1]][number[2]] = -1;
counterKO = counterKO + 1;
animateCounter('img-counter-ko');
writeById('counter-ko',counterKO);
}
}
function eventHub(e) {
const ASCII_0 = 48;
const ASCII_9 = 57;
const ASCII_BACKSPACE = 8;
if (GLOBAL_finished==1) return;
switch (e.type) {
case 'keyup':
if(e.which >=ASCII_0 && e.which<=ASCII_9){// NUMBER key pressed
var digit = e.which - ASCII_0;
processDigit(digit);
} else if (e.which==ASCII_BACKSPACE){
clearGuess();
}
break;
case 'click':
var digit = e.srcElement.innerText;
if (digit>='0' && digit<='9') {
processDigit(parseInt(digit));
} else if (digit=='Borrar') {
clearGuess();
}
break;
}
}
function addEvent(element, eventName, callback) {
if (element.addEventListener) {
element.addEventListener(eventName, callback, false);
} else if (element.attachEvent) {
element.attachEvent("on" + eventName, callback);
}
}
function showNumberContent(index, content) {
writeById('number-' + index, content);
}
/* Generate result table, PLUS its representation */
function generateResultTable(firstNumber, secondNumber) {
resultTable = new Array(firstNumber);
var table = document.getElementById('result-table');
for (var y=0;y<=firstNumber;y++) {
var row = table.insertRow(y);
resultTable[y] = new Array(secondNumber);
for (var x=0;x<=secondNumber;x++) {
var cell = row.insertCell(x);
cell.id = 'result-' + y + '-' + x;
cell.innerHTML = y + 'x' + x;
if (GLOBAL_removeZeroes==1 && (x==0 || y==0)) {
resultTable[y][x] = -2; // -2 means "NOT ASKED"
cell.className = 'cell-notasked';
} else {
resultTable[y][x] = 0; // 0 means "not set yet"
cell.className = 'cell-pending';
}
}
}
}
/* Utils functions - domain agnostic */
function writeById(id,content) {
document.getElementById(id).innerHTML = content;
}
function getParameterByName(name, defaultValue) {
var url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return defaultValue;
if (!results[2]) return defaultValue;
var paramValue = decodeURIComponent(results[2].replace(/\+/g, " "));
if (paramValue) {
return paramValue;
} else {
return defaultValue;
}
}
/**
* Returns a random integer between min (inclusive) and max (inclusive)
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}