forked from kecav/math-quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
193 lines (173 loc) · 5.23 KB
/
script.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
let n1;
let n2;
let opSelector;
let ansOpt;
let answer;
let qNo = document.getElementById("Qno");
let score = document.getElementById("score");
let question = document.getElementById("question");
let buttons = document.getElementsByTagName("button");
let start = document.getElementById("start-btn");
let fScore = document.getElementById("final-score");
let startBox = document.getElementById("start-game");
let gameBox = document.getElementById("in-game");
let endBox = document.getElementById("end-game");
let progress = document.getElementById("progress");
let message = document.getElementById("message");
let operator = ['+', '-', '*', '/'];
let t;
function restart() {
score.innerHTML = "0";
qNo.innerHTML = "0";
nextQuestion();
gameBox.style.display = "block"
startBox.style.display = "none";
endBox.style.display = "none";
timer.style.display = "block";
}
function whenFinished() {
console.log("Finished.")
gameBox.style.display = "none"
startBox.style.display = "none";
endBox.style.display = "flex";
lastmessage();
}
function nextQuestion() {
progress.style.width = "100%";
timed();
// timed();
fScore.innerHTML = score.innerHTML;
if (qNo.innerText == "10") {
whenFinished();
}
n1 = Math.floor(Math.random() * 100);
n2 = Math.floor(Math.random() * 100);
opSelector = operator[Math.floor(Math.random() * 4)];
if (opSelector == "/") {
for (let i = 0; i < 200; i++) {
if (n1 % n2 == 0 && n1 != 0 && n2 != 0 && n2 != 1 && n1 != n2) {
break;
}
n1 = Math.floor(Math.random() * 100);
n2 = Math.floor(Math.random() * 100);
}
}
if (opSelector == "*") {
for (let i = 0; i < 100; i++) {
if (n1 * n2 <= 1000) {
break;
}
n1 = Math.floor(Math.random() * 50);
n2 = Math.floor(Math.random() * 50);
}
}
question.innerHTML = n1 + opSelector + n2;
answer = eval(question.innerHTML);
question.innerHTML = question.innerHTML + " = ?";
// console.log("answer: " + answer);
getOptions();
getQNo();
}
function getOptions() {
for (let i = 0; i < 4; i++ && i != ansOpt) {
if (answer > 100) {
buttons[i].innerHTML = answer + Math.floor(Math.random() * answer * 0.4);
} else if (answer > 30 && answer < 100) {
buttons[i].innerHTML = answer + Math.floor(Math.random() * answer * 0.6);
} else {
buttons[i].innerHTML = Math.floor(Math.random() * 100);
}
if (answer < 0) {
buttons[i].innerHTML = "-" + buttons[i].innerHTML;
}
}
ansOpt = Math.floor(Math.random() * 4);
buttons[ansOpt].innerHTML = answer;
}
function getQNo() {
qNo.innerHTML = parseInt(qNo.innerHTML) + 1;
// console.log("Q no: " + qNo.innerHTML);
}
function getScore() {
score.innerHTML = parseInt(score.innerHTML) + parseInt(progress.style.width);
// console.log(score.innerHTML);
}
function doWhenCorrect(i) {
buttons[i].style.color = "#fff";
buttons[i].style.backgroundColor = "green";
getScore();
}
function doWhenIncorrect(i) {
buttons[i].style.color = "#fff";
buttons[i].style.backgroundColor = "#fb3640";
// console.log("wrong");
}
function outro(i) {
setTimeout(() => {
nextQuestion();
buttons[i].style.color = "#000";
buttons[i].style.backgroundColor = "rgba(0, 0, 0, 0.1)";
}, 500);
}
function lastmessage() {
clearInterval(t);
if (fScore.innerText >= 800) {
let emoji = "😍";
message.innerHTML = "WOW !! UNBELIEVABLE !!" + emoji;
} else if (fScore.innerText >= 500) {
let emoji = "😓";
message.innerHTML = "TOO CLOSE !!" + emoji;
} else if (fScore.innerText >= 100) {
let emoji = "😥";
message.innerHTML = "Better luck next time " + emoji;
} else {
let emoji = "🙁";
message.innerHTML = "Bad Luck " + emoji;
}
}
function timed() {
t = setInterval(() => {
progress.style.width = (parseInt(progress.style.width) - 1) + "%";
console.log("called");
if (parseInt(progress.style.width) == 0) {
clearInterval(t);
nextQuestion();
}
}, 100)
}
buttons[0].addEventListener('click', () => {
if (buttons[0].innerText == answer) {
doWhenCorrect(0);
} else {
doWhenIncorrect(0);
}
clearInterval(t);
outro(0);
});
buttons[1].addEventListener('click', () => {
if (buttons[1].innerText == answer) {
doWhenCorrect(1);
} else {
doWhenIncorrect(1);
}
clearInterval(t);
outro(1);
});
buttons[2].addEventListener('click', () => {
if (buttons[2].innerText == answer) {
doWhenCorrect(2);
} else {
doWhenIncorrect(2);;
}
clearInterval(t);
outro(2);
});
buttons[3].addEventListener('click', () => {
if (buttons[3].innerText == answer) {
doWhenCorrect(3);
} else {
doWhenIncorrect(3);
}
clearInterval(t);
outro(3);
});