-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
230 lines (203 loc) · 8 KB
/
index.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
// Number generator
function numberGenerator(generateNoUpto) {
let random = Math.floor(Math.random() * generateNoUpto + 1);
return random;
}
// Operator Generator
function operatorGenerator() {
let random = Math.floor(Math.random() * 4);
let operand = ["+", "-", "*", "/"];
return operand[random];
}
// questionGenerator function arguments
// x: how many operator we need in equation
// y: number upto which operand number must be
// n: if we need same type of operator or random operators
// n = either (1 or 0) or (true or false) 1 or true for same operator
// o: if "n" is true then it take operator which we want such as "+", "-", "*", "/"
// example: questionGenerator(2, 455, true, "*");
// Question generator
function questionGenerator(x, y, n, o) {
let operator = [];
let operand = [];
let q = "";
for (let i = 0; i < x; i++) {
operator.push(operatorGenerator());
};
for (let i = 0; i < x + 1; i++) {
operand.push(numberGenerator(y));
};
if (n === 1 || n === true) {
for (let i = 0; i < x + 1; i++) {
if (i < x) {
q += operand[i] + o;
} else {
q += operand[i];
}
}
} else {
for (let i = 0; i < x + 1; i++) {
if (i < x) {
q += operand[i] + operator[i];
} else {
q += operand[i];
}
}
}
//Option Generator
function optionGenerator(q) {
let value = "";
if (eval(q) >= (-1) || eval(q) <= 1) {
value = eval(eval(q) + operatorGenerator() + Math.random());
} else {
value = eval(eval(q) + operatorGenerator + Math.random(Math.pow(10, (Math.trunc(eval(q))).toString().length)));
}
return value;
}
// Handling option generation
let option = ["", "", "", ""];
let corr = Math.floor(Math.random() * 3);
if ((eval(q) > 1 || eval(q) < -1) && eval(q) !== Math.trunc(eval(q))) {
option[corr] = eval(q).toFixed(2);
} else {
option[corr] = eval(q);
}
for (let i = 0; i < 4; i++) {
// Requiire better option generation Algorithm
if (i !== (corr)) {
if (eval(q) === Math.trunc(eval(q))) {
option[i] = Math.floor(parseInt(optionGenerator(q)));
// require better option generation techniqe
while (option[i] == option[corr]) {
option[i] = Math.floor(parseInt(optionGenerator(q)));
}
} else if ((eval(q) > 1 || eval(q) < -1) && eval(q) !== Math.trunc(eval(q))) {
option[i] = Number.parseFloat(optionGenerator(q)).toFixed(2);
} else {
option[i] = optionGenerator(q);;
}
}
}
// sending questions and options to object
data.question.push(q);
data.option.push(option);
data.correctOption.push(corr);
}
// Oject for storing data
let data = {
level: "",
question: [],
option: [],
correctOption: [],
choosenOption: []
}
// Dom manipulations
let score = 0;
let percentScore = 0;
let counter = 0;
let pressCounter = 0;
//on loading function
window.onload = load;
function load() {
document.getElementById("landing").style.display = "initial";
document.getElementById("menu").style.display = "none";
document.getElementById("quiz").style.display = "none";
document.getElementById("result").style.display = "none";
}
// landing Page
function landingPage() {
document.getElementById("landing").style.display = "none";
document.getElementById("menu").style.display = "initial";
document.getElementById("quiz").style.display = "none";
document.getElementById("result").style.display = "none";
}
function start() {
document.getElementById("landing").style.display = "none";
document.getElementById("menu").style.display = "none";
document.getElementById("quiz").style.display = "initial";
document.getElementById("result").style.display = "none";
render();
}
// Warning: This function is recursive so we have to fix that
function render() {
pressCounter += 1;
if (counter < 9) {
questionGenerator(1, 100);
document.getElementById("question").innerHTML = "Q" + (counter + 1) + ". What is value of " + data.question[counter];
document.getElementById("0").innerHTML = "(a). " + data.option[counter][0];
document.getElementById("1").innerHTML = "(b). " + data.option[counter][1];
document.getElementById("2").innerHTML = "(c). " + data.option[counter][2];
document.getElementById("3").innerHTML = "(d). " + data.option[counter][3];
counter += 1;
caller();
} else if (counter === 9) {
questionGenerator(1, 100);
document.getElementById("question").innerHTML = "Q" + (counter + 1) + ". What is value of " + data.question[counter];
document.getElementById("0").innerHTML = "(a). " + data.option[counter][0];
document.getElementById("1").innerHTML = "(b). " + data.option[counter][1];
document.getElementById("2").innerHTML = "(c). " + data.option[counter][2];
document.getElementById("3").innerHTML = "(d). " + data.option[counter][3];
counter = 0;
setTimeout(() => {
document.getElementById("landing").style.display = "none";
document.getElementById("menu").style.display = "none";
document.getElementById("quiz").style.display = "none";
document.getElementById("result").style.display = "initial";
}, 10000);
}
// console logs
console.log("Question:", (counter), data.question[counter - 1]);
console.log("Answer: ", eval(data.question[counter - 1]));
console.log("Options: ", data.option);
console.log("correct options", data.correctOption);
console.log("choosen option", data.choosenOption);
}
let interval;
function caller() {
interval = setTimeout(() => {
render();
}, 10000);
}
function optionButtonClick(click) {
if (pressCounter < 10) {
if (data.option[pressCounter - 1][data.correctOption[pressCounter - 1]] == data.option[pressCounter - 1][click]) {
document.getElementById(click).style.backgroundColor = "green"
document.getElementById(click).style.color = "white"
score += 10;
} else {
document.getElementById(click).style.backgroundColor = "red"
document.getElementById(click).style.color = "white"
}
setTimeout(() => {
clearInterval(interval);
render();
document.getElementById(click).style.backgroundColor = "initial"
document.getElementById(click).style.color = "initial"
}, 200);
} else if (pressCounter === 10) {
if (data.option[pressCounter - 1][data.correctOption[pressCounter - 1]] == data.option[pressCounter - 1][click]) {
document.getElementById(click).style.backgroundColor = "green"
document.getElementById(click).style.color = "white"
score += 10;
} else {
document.getElementById(click).style.backgroundColor = "red"
document.getElementById(click).style.color = "white"
}
setTimeout(() => {
document.getElementById(click).style.backgroundColor = "initial"
document.getElementById(click).style.color = "initial"
document.getElementById("landing").style.display = "none";
document.getElementById("menu").style.display = "none";
document.getElementById("quiz").style.display = "none";
document.getElementById("result").style.display = "initial";
resultPage();
}, 200);
pressCounter = 0;
}
}
function resultPage() {
document.getElementById("correct").innerHTML = "Correct: " + (score/10);
document.getElementById("incorrect").innerHTML = "InCorrect: " + (10-(score/10));
document.getElementById("score").innerHTML = "score: " + score;
document.getElementById("percentScore").innerHTML = "You were " + score + "% accurate";
}