-
Notifications
You must be signed in to change notification settings - Fork 1
/
maths.js
42 lines (35 loc) · 1.27 KB
/
maths.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
var answer;
var score = 0;
var backgroundImages = [];
function nextQuestion() {
const n1 = Math.floor(Math.random() * 5);
document.getElementById('n1').innerHTML = n1;
const n2 = Math.floor(Math.random() * 6);
document.getElementById('n2').innerHTML = n2;
answer = n1 + n2;
}
function checkAnswer() {
const prediction = predictImage();
console.log(`answer: ${answer}, prediction: ${prediction}`);
if (prediction == answer) {
score++;
console.log(`Correct. Score ${score}`);
if (score <= 6) {
backgroundImages.push(`url('images/background${score}.svg')`);
document.body.style.backgroundImage = backgroundImages;
} else {
alert('Well done! Your math garden is in full bloom! Want to start again?');
score = 0;
backgroundImages = [];
document.body.style.backgroundImage = backgroundImages;
}
} else {
if (score != 0) {score--;}
console.log(`Wrong. Score ${score}`);
alert('Oops! Check your calculations and try writing the number neater next time!');
setTimeout(function () {
backgroundImages.pop();
document.body.style.backgroundImage = backgroundImages;
}, 1000);
}
}