-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathExercice.js
357 lines (179 loc) · 6.53 KB
/
MathExercice.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//function for display the number in the input
function PrintNumber(e){
document.querySelector('input').value += e;
}
//function to deleted a number
function Sub(){
var input = document.querySelector('input');
if(input.value.length === 0){
//do nothing
}else{
input.value = input.value.substring(0, input.value.length-1);
}
}
//function to create aleatory operation
var progress = document.querySelector('progress');
var lives = 3;
var level = 0;
var op;
var calculations;
function CreateCalc(){
//define operation aleatory
var op = ['+', '-', '*', '/'];
operation = op[Math.trunc(Math.random()*4)];
//define two numbers aleatory
var first;
var twice;
//define the level of the op
switch (operation) {
case '*':
first = Math.trunc(Math.random()*10);
twice = Math.trunc(Math.random()*(10+(2*level)));
break;
case '/':
first = Math.trunc(Math.random()*4+level);
twice = Math.trunc(Math.random()*(4));
//check if calculations are 0
if(first === 0){
first = 2;
}
if(twice === 0){
twice = 2;
}
//check if the calclulations are even or odd
if(first % 2 === 1){
first++;
}
if(twice % 2 === 1){
twice++;
}
//check if twice is bigger than first
if(twice > first){
let a = twice;
twice = first;
first = a;
}
//check for the result can't be with decimal
if(eval(first/twice).toString().includes('.')){
first += 2;
}
break;
case '-':
first = Math.trunc(Math.random()*(20*(level === 0 ? 1 : level)));
do{
twice = Math.trunc(Math.random()*(20*(level === 0 ? 1 : level)));
}while((first - twice) < 0);
break;
default:
first = Math.trunc(Math.random()*(40*(level === 0 ? 1 : level)));
twice = Math.trunc(Math.random()*(40*(level === 0 ? 1 : level)));
break;
}
//create the calculations
calculations = `${first} ${operation} ${twice}`;
//display the result to span
document.getElementById('calcul').textContent = calculations;
}
//call the function for the first calclution
CreateCalc();
//function to send the calcul
document.getElementById('send-calc').addEventListener('click', ()=>{
//function to take of a live
function LessLives(){
//set the animation
document.getElementById('lives').style.animation = 'oof 3s 1';
document.getElementById('less-lives').style.animation = 'opAnimation 3s 1';
lives--;
//display lives of the player
document.getElementById('lives').textContent = lives;
//reset the
setTimeout(()=>{
document.getElementById('lives').style.animation = '';
document.getElementById('less-lives').style.animation = '';
}, 3001);
}
//function if the user loose
function Loose(){
var random_msg = ['Sorry :[', 'Not today', 'Miss !', 'Oops, its the wrong way', 'Not yet', 'Later, maybe...', 'Almost ;)'];
document.querySelector('.loose-msg').textContent = random_msg[Math.trunc(Math.random()*random_msg.length)];
document.querySelector('.loose-msg').style.display = 'block';
for (let i = 0; i < 12; i++) {
document.querySelectorAll('.num')[i].disabled = true;
document.getElementById('send-calc').disabled = true;
}
}
//function to win and pass level
function Win(){
progress.value += 1;
if(progress.value === progress.max){
//met un message
document.querySelector('.head').children[0].textContent = `Wow, you are level : ${level+1}`;
//reset le msg au bout de 4s;
setTimeout(()=>{
document.querySelector('.head').children[0].textContent = 'Math Exercice';
}, 4000);
//set the progress bar at himself*2
progress.max*=2;
level++;
//display the level of the player
document.getElementById('level').innerText = level;
//set the animation
document.getElementById('level').style.animation = 'win 3s 1';
document.getElementById('next-level').style.animation = 'opAnimation 3s 1';
//reset l'animation au bout de 3s
setTimeout(()=>{
document.getElementById('level').style.animation = '';
document.getElementById('next-level').style.animation = '';
}, 3001);
}else{
//do nothing
}
//display result progress bar
document.getElementById('value-pro').textContent = progress.value;
document.getElementById('max-pro').textContent = progress.max;
}
//rest of the function
var input = document.querySelector('input');
if(input.value.length === 0){
//check the value
alert('You did not enter a value');
}else{
//check the calcul
if(eval(input.value) === eval(calculations)){
Win();
}else{
LessLives();
if(lives < 0){
Loose();
}
}
}
//pass calc
CreateCalc();
input.value = '';
});
//create the number pad
(function(){
//create the tr elements
for(let i = 0; i < 4; i++){
document.getElementById('pad').append(document.createElement('tr'));
}
//create the td elements
let lign = -1;
for(let i = 1; i < 10; i++){
let number = document.createElement('button'); number.setAttribute('class', 'num'); number.setAttribute('onclick', `PrintNumber(${i})`); number.textContent = i;
if((i-1) % 3 === 0){
lign += 1;
}
document.getElementById('pad').children[lign].append(number);
}
//create the zero and the > pad
var choice = ['Sub()', '>', `PrintNumber(${0})`, 0];
for(let i = 0; i < 3 ;i++){
if(i === 1){
i++;
}
let number = document.createElement('button'); number.setAttribute('class', 'sub'); number.setAttribute('onclick', choice[i]); number.textContent = choice[i+1];
document.getElementById('pad').children[3].append(number);
}
})();