-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
326 lines (259 loc) · 7.6 KB
/
main.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
// select all elements
//const start = document.getElementById("start");
const quiz = document.getElementById("quiz");
const question = document.getElementById("question");
const qImg = document.getElementById("qImg");
const choiceA = document.getElementById("A");
const choiceB = document.getElementById("B");
const choiceC = document.getElementById("C");
const counter = document.getElementById("counter");
const timeGauge = document.getElementById("timeGauge");
const progress = document.getElementById("progress");
const scoreDiv = document.getElementById("scoreContainer");
const starDiv = document.getElementById("starContainer");
const qlevel = document.getElementById("qlevel");
const levels = document.getElementById("levels");
const yessound = document.getElementById("yesaudio");
const nosound = document.getElementById("noaudio");
const levelreportDiv = document.getElementById("levelreport");
const scorereportDiv = document.getElementById("scorereport");
const qaudio = document.getElementById('questionaudio');
yessound.load();
nosound.load();
// create some variables
//const lastQuestion = questions.length - 1;
const lastQuestion = 10 -1; //asking at most ten questions
let runningQuestion = 0;
let count = 0;
const questionTime = 10; // 10 seconds for each question
const gaugeWidth = 150; // 150px
const gaugeUnit = gaugeWidth / questionTime;
let TIMER;
let score = 0;
let currentlevel=1;
//document.getElementById("lv1").style.backgroundColor = "lightblue";
// getting questions from outside files
//var questions = [];
function setDifficulty(level){
currentlevel = level;
//alert(currentlevel);
//document.getElementById("lv1").style.backgroundColor = "white";
//document.getElementById("lv2").style.backgroundColor = "white";
//document.getElementById("lv3").style.backgroundColor = "white";
//document.getElementById("lv"+level).style.backgroundColor = "lightblue";
chineselevel = "一"
switch(currentlevel) {
case 1:
chineselevel = "一";
break;
case 2:
chineselevel = "二";
break;
case 3:
chineselevel = "三";
break;
case 4:
chineselevel = "四";
break;
case 5:
chineselevel = "五";
break;
}
qlevel.innerHTML = chineselevel + "級";
startQuiz(level)
}
// difficulty level
function generateQuestion(){
//additionQ = additionQ.filter(item => item.level ==currentlevel);
//subtractionQ = subtractionQ.filter(item => item.level == currentlevel);
//multiplicationQ = multiplicationQ.filter(item => item.level ==currentlevel);
//divisionQ = divisionQ.filter(item => item.level ==currentlevel);
questions = questionbank.filter(item => item.level == currentlevel);
//questions.push(...additionQ,...subtractionQ,...multiplicationQ,...divisionQ)
shuffleArray(questions);
}
// randomize question
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
// render a question
function renderQuestion(){
let q = questions[runningQuestion];
//question.innerHTML = "<p>"+ q.question +"</p>";
//qImg.innerHTML = "<img src="+ q.imgSrc +">";
//alert(q.oggSrc);
qaudio.src = q.oggSrc;
setTimeout(playaudio,1500);
choiceA.innerHTML = "<p> A. " + q.choiceA +"</p>";
choiceB.innerHTML = "<p> B. " + q.choiceB +"</p>";
choiceC.innerHTML = "<p> C. " + q.choiceC +"</p>";
}
function playaudio(){
qaudio.play();
}
question.addEventListener("mouseover",playaudio);
// start quiz
function startQuiz(level){
start.style.display = "none";
levels.style.display = "none";
generateQuestion();
renderQuestion();
quiz.style.display = "block";
renderProgress();
renderCounter();
TIMER = setInterval(renderCounter,1000); // 1000ms = 1s
}
// restart at the given level
function mainmenu(){
scoreDiv.style.display = "none";
quiz.style.display = "none";
start.style.display = "block";
levels.style.display = "block";
runningQuestion = 0;
count = 0;
score = 0;
progress.innerHTML = "";
}
// restart at the given level
function restart(level){
scoreDiv.style.display = "none";
runningQuestion = 0;
count = 0;
score = 0;
progress.innerHTML = "";
startQuiz(level);
}
// restart with level up
function levelup(){
if (currentlevel < 5){
currentlevel++;
}
chineselevel = "一"
switch(currentlevel) {
case 1:
chineselevel = "一";
break;
case 2:
chineselevel = "二";
break;
case 3:
chineselevel = "三";
break;
case 4:
chineselevel = "四";
break;
case 5:
chineselevel = "五";
break;
}
qlevel.innerHTML = chineselevel + "級";
//alert(currentlevel);
restart(currentlevel);
}
// render progress
function renderProgress(){
for(let qIndex = 0; qIndex <= lastQuestion; qIndex++){
progress.innerHTML += "<div class='prog' id="+ qIndex +"></div>";
}
}
// counter render
function renderCounter(){
if(count <= questionTime){
counter.innerHTML = count;
timeGauge.style.width = count * gaugeUnit + "px";
count++
}else{
count = 0;
// change progress color to red
answerIsWrong();
if(runningQuestion < lastQuestion){
runningQuestion++;
renderQuestion();
}else{
// end the quiz and show the score
clearInterval(TIMER);
scoreRender();
}
}
}
function checkAnswer(answer){
// checkAnswer
if( answer == questions[runningQuestion].correct){
// answer is correct
score++;
// change progress color to green
answerIsCorrect();
yessound.play();
}else{
// answer is wrong
// change progress color to red
answerIsWrong();
nosound.play();
}
count = 0;
if(runningQuestion < lastQuestion){
runningQuestion++;
renderQuestion();
}else{
// end the quiz and show the score
clearInterval(TIMER);
scoreRender();
}
}
// answer is correct
function answerIsCorrect(){
document.getElementById(runningQuestion).style.backgroundColor = "#ffcc00";
}
// answer is Wrong
function answerIsWrong(){
document.getElementById(runningQuestion).style.backgroundColor = "#e4e4e4";
document.getElementById(runningQuestion).style.borderColor = "#e4e4e4";
}
// score render
function scoreRender(){
scoreDiv.style.display = "block";
// calculate the amount of question percent answered by the user
const scorePerCent = Math.round(100 * score/(lastQuestion+1));
const yellowstar = Math.round(scorePerCent / 10);
const graystar = 10 - yellowstar;
// choose the image based on the scorePerCent
levelreportDiv.innerHTML = "<p> "
chineselevel = "一"
switch(currentlevel) {
case 1:
chineselevel = "一";
break;
case 2:
chineselevel = "二";
break;
case 3:
chineselevel = "三";
break;
case 4:
chineselevel = "四";
break;
case 5:
chineselevel = "五";
break;
}
levelreportDiv.innerHTML += currentlevel + "級</p>";
scorereportDiv.innerHTML = "<p> Score: "+ scorePerCent + "</p>";
starDiv.innerHTML = "";
//alert(yellowstar);
//starDiv.innerHTML += "<img class='inline-block' src='img/GoldenStar.png'>";
//starDiv.innerHTML += "<img class='inline-block' src='img/GoldenStar.png'>";
for (let counter= 0;counter < yellowstar; counter++){
starDiv.innerHTML += "<img src='img/GoldenStar.png'>";
//alert(counter);
}
for (let counter= 0;counter < graystar; counter++){
starDiv.innerHTML += "<img src='img/GrayStar.png'>";
//alert(counter);
}
//scoreDiv.innerHTML = "<img src="+ img +">";
//scoreDiv.innerHTML = ""
//scoreDiv.innerHTML += "<p>"+ scorePerCent +"%</p>";
}