-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubtract.js
71 lines (59 loc) · 1.78 KB
/
subtract.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
const option1 = document.getElementById("option1"),
option2 = document.getElementById("option2"),
option3 = document.getElementById("option3"),
correct = document.getElementById("correct"),
wrong = document.getElementById("wrong");
var answer = 0;
function generate_equation(){
var num1 = Math.floor(Math.random() * 11),
num2 = Math.floor(Math.random() * 11),
dummyAnswer1 = Math.floor(Math.random() * 10),
dummyAnswer2 = Math.floor(Math.random() * 10),
allAnswers = [],
switchAnswers = [];
if(num1 > num2){
answer = eval(num1 - num2);
document.getElementById("num1").innerHTML = num1;
document.getElementById("num2").innerHTML = num2;
}
else{
answer = eval(num2 - num1);
document.getElementById("num1").innerHTML = num2;
document.getElementById("num2").innerHTML = num1;
}
allAnswers = [answer, dummyAnswer1, dummyAnswer2];
for (i = allAnswers.length; i--;){
switchAnswers.push(allAnswers.splice(Math.floor(Math.random() * (i + 1)), 1)[0]);
};
option1.innerHTML = switchAnswers[0];
option2.innerHTML = switchAnswers[1];
option3.innerHTML = switchAnswers[2];
};
option1.addEventListener("click", function(){
if(option1.innerHTML == answer){
correct.play();
generate_equation();
}
else{
wrong.play();
}
});
option2.addEventListener("click", function(){
if(option2.innerHTML == answer){
correct.play();
generate_equation();
}
else{
wrong.play();
}
});
option3.addEventListener("click", function(){
if(option3.innerHTML == answer){
correct.play();
generate_equation();
}
else{
wrong.play();
}
});
generate_equation()