-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
216 lines (170 loc) · 6.19 KB
/
game.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
//grab all
const seconds = document.querySelector(".timerseconds");
let countdown = 20;
let progress = document.querySelector("progress");
const options = document.querySelector(".options");
const optionslist = document.querySelectorAll(".options ul li");
const scorevalue = document.querySelector(".scorevalue");
const img = document.querySelector("img");
let totalscore = 0;
let question = 1;
const questiondisplay = document.querySelector(".questiondisplay");
let beginnerarray, intermediatearray, expertarray;
let randomnumber;
let currentarray;
let countdowntime;
let displayquestionnumber = document.querySelector(".displayquestionnumber");
let displaytotalquestions = document.querySelector(".displaytotalquestions");
// display total number of questions
displaytotalquestions.textContent = "5";
//to prevent errors
localStorage.setItem("currentscore",-2);
//setup timer
function countdowntimer() {
countdowntime = setInterval(() => {
countdown--;
seconds.textContent = `${countdown}`;
if (countdown === 0) {
showrightanswer();
clearInterval(countdowntime);
if (question < 6) {
setTimeout(() => {
seconds.classList.remove("lowontime");
question++;
//remove previous question from array
currentarray.splice(randomnumber,1);
displayquestions();
countdown = 21;
countdowntimer();
removeclasslist();
}, 1500)
}
}
if (countdown < 11) {
seconds.classList.add("lowontime");
}
}, 800)
}
countdowntimer();
//fetch all questions from json file and store them in array
//function to fetch beginner question
fetch("questions/beginner.json")
.then((data) => {
return data.json();
}).then((data) => {
beginnerarray = data.questions;
displayquestions();
})
//function to fetch intermediate questions
fetch("questions/intermediate.json")
.then((data) => {
return data.json();
}).then((data) => {
intermediatearray = data.questions;
})
//function to fetch expert questions
fetch("questions/expert.json")
.then((data) => {
return data.json();
}).then((data) => {
expertarray = data.questions;
})
//function to show right answer after clicking
options.addEventListener("click", (e) => {
if (e.target.tagName === "LI") {
let option = e.target.getAttribute("data-option");
if (option == currentarray[randomnumber].correctIndex) {
;
e.target.classList.add("right");
totalscore += 100;
scorevalue.textContent = totalscore;
}
optionslist.forEach((option) => {
option.classList.add("clicked");
if (option.getAttribute("data-option") == currentarray[randomnumber].correctIndex) {
option.classList.add("right");
}
clearInterval(countdowntime);
})
if (question < 6) {
setTimeout(() => {
question++;
countdown = 21 ;
seconds.classList.remove("lowontime");
countdowntimer();
removeclasslist(option);
//remove previous question from array
currentarray.splice(randomnumber,1);
displayquestions();
}, 1500)
}
else {
//jump to end html file which is to be created
window.location.assign('./end.html')
// window.location.href = "end.html"
}
}
})
//function to show right answer when time runs out
function showrightanswer() {
let rightanswer = currentarray[randomnumber].correctIndex;
optionslist.forEach((option) => {
if (option.getAttribute("data-option") == rightanswer) {
option.classList.add("right");
}
option.classList.add("clicked");
})
}
//when page loads show a beginner question
const displayquestions = () => {
//display from beginner question
if (question < 3) {
currentarray = beginnerarray
randomnumber = Math.floor(Math.random() * beginnerarray.length);
questiondisplay.textContent = beginnerarray[randomnumber].question;
optionslist.forEach((option, index) => {
option.textContent = beginnerarray[randomnumber].answers[index];
img.src = `${beginnerarray[randomnumber].url}`;
})
}
//display from intermediate question
else if (question === 3 || question === 4) {
currentarray = intermediatearray
randomnumber = Math.floor(Math.random() * intermediatearray.length);
questiondisplay.textContent = intermediatearray[randomnumber].question;
optionslist.forEach((option, index) => {
option.textContent = intermediatearray[randomnumber].answers[index];
img.src = `${intermediatearray[randomnumber].url}`;
})
}
//display from expert question
if (question === 5) {
currentarray = expertarray
randomnumber = Math.floor(Math.random() * expertarray.length);
questiondisplay.textContent = expertarray[randomnumber].question;
optionslist.forEach((option, index) => {
option.textContent = expertarray[randomnumber].answers[index];
img.src = `${expertarray[randomnumber].url}`;
})
}
//this to jump to end.html file
if (question === 6) {
//save score to local storage
localStorage.setItem("currentscore",totalscore);
//go to end page
// window.location.href = "end.html"
window.location.assign('./end.html')
}
if (question < 6) {
displayquestionnumber.textContent = question;
progress.value = question;
}
}
//function to remove the classlist(the red and green color on options)before displaying next question
const removeclasslist = () => {
seconds.textContent = `20`;
optionslist.forEach((option) => {
option.classList.remove("clicked");
option.classList.remove("right");
})
}