-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
291 lines (283 loc) · 10.9 KB
/
script.js
File metadata and controls
291 lines (283 loc) · 10.9 KB
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
let quizData = {
type:{
'Fun Quiz':{
q1:{
question: 'What is the most common colour of toilet paper in France?',
choices:['Pink', 'Blue', 'White', 'Red'],
corectIdx: 0
},
q2:{
question: 'The average person does what thirteen times a day?',
choices: ['Yawns','Laughs','Smiles','Jumps'],
corectIdx: 1
},
q3:{
question: 'Coprastastaphobia is the fear of what?',
choices:['Heights','Flying','Constipation','Spiders'],
corectIdx: 2
},
q4:{
question: 'It\'s illegal in Texas to put what on your neighbour’s Cow?',
choices:['Marshmallows','Candy','Sprinkles','Grafitti'],
corectIdx: 3
},
q5:{
question: 'Native to the Caribbean, what sort of animal is the mountain chicken?',
choices:['Bird','Dog','Bear','Frog','Snake','Bat'],
corectIdx: 3
},
q6:{
question: 'True or false: You can sneeze in your sleep?',
choices:['True','False'],
corectIdx: 1
},
q7:{
question: 'Which European country has 158 verses to its national anthem?',
choices:['Germay','Greece','Italy','Spain'],
corectIdx: 1
},
q8:{
question: 'What is Scooby Doo’s full name?',
choices:['Scot Doo Doo','Albert Scoob Doo','Scoobert Doo','Scoobert Doo Doo'],
corectIdx: 2
},
q9:{
question: 'Where was the fortune cookie invenindexted?',
choices:['Beijing','Tokyo','Budapest','San Francisco'],
corectIdx: 3
},
q10:{
question: 'Which country has the most tornadoes by area?',
choices:['England','Greece','USA','Japan'],
corectIdx: 0
}
},
'Cities':{
q1:{
question: 'Which city does not have an obelisk?',
choices:['Rome', 'Ulaanbataar', 'London', 'Cairo'],
corectIdx: 1
},
q2:{
question: 'In what city was the first skyscraper built?',
choices:['Durban', 'London', 'Hong Kong', 'Chicago'],
corectIdx: 3
},
q3:{
question: 'What city is well known for its Carnival?',
choices:['Khartoum', 'Rio de Janeiro', 'Kiev', 'Colombo'],
corectIdx: 1
},
q4:{
question: 'What Mexican port is famous for its cliff divers?',
choices:['Tijuana', 'Acapulco', 'Managua', 'Tampico'],
corectIdx: 1
},
q5:{
question: 'At the heart of which city can a commons be found?',
choices:['Trenton', 'Philadelphia', 'New York City', 'Boston'],
corectIdx: 3
},
q6:{
question: 'What is the only major world capital named after a religion?',
choices:['Christiansted', 'Christiansburg', 'Seoul', 'Islamabad'],
corectIdx: 3
},
q7:{
question: 'What is the largest city in Turkey?',
choices:['Adana', 'Damascus', 'Istanbul', 'Ankara'],
corectIdx: 2
},
q8:{
question: 'What is the citadel of Moscow called?',
choices:['Ginza', 'Tiananmen Square', 'Empire State Building', 'Kremlin'],
corectIdx: 3
},
q9:{
question: 'Which Indian city is famed for its clock tower?',
choices:['Delhi', 'Kolkata', 'Pune', 'Mumbai'],
corectIdx: 3
},
q10:{
question: 'Where might one find the city of Tunis?',
choices:['Tunisia', 'Algeria', 'Egypt', 'Kenya'],
corectIdx: 0
}
},
'Countries by Pictures':{
q1: {
question: 'Guess the countries that are depicted in these images.',
choices:['Russia','Japan','Norway','Sweden'],
corectIdx: 3,
img: 'img/sweden.jpg'
},
q2: {
question: 'Guess the countries that are depicted in these images.',
choices:['Taiwan','Japan','South Korea','China'],
corectIdx: 0,
img: 'img/taiwan.jpg'
},
q3: {
question: 'Guess the countries that are depicted in these images.',
choices:['Japan','South Korea','China','Taiwan'],
corectIdx: 1,
img: 'img/southKorea.jpg'
},
q4: {
question: 'Guess the countries that are depicted in these images.',
choices:['Portugal','U.S.A','England','Australia'],
corectIdx: 3,
img: 'img/australia.jpg'
},
q5: {
question: 'Guess the countries that are depicted in these images.',
choices:['Slovakia','Russia','Serbia','Hungary'],
corectIdx: 1,
img: 'img/russia.jpg'
},
q6: {
question: 'Guess the countries that are depicted in these images.',
choices:['Czech Republic','Austria','Estonia','Germany'],
corectIdx: 0,
img: 'img/czechRepublic.jpg'
},
}
},
score: 0,
playingQuizArr: [],
resetGame:()=>{
quizData.score = 0;
quizData.playingQuizArr = [];
},
quizType:()=>{
let types=[];
for(let type in quizData.type){
types.push(type);
}
return types;
},
generateNewQuizArr: (type)=>{
Object.keys(quizData.type[type]).forEach(key => quizData.playingQuizArr.push(key));
},
getAnswer: (qCorrectIdx, AnswerIndex,qIndexInArr) =>{
let result;
//check result and update score
if(qCorrectIdx==AnswerIndex){
result= true; quizData.score++;
}else{
result= false;
}
//remove displayed question in the playinglist
quizData.playingQuizArr.splice(qIndexInArr,1);
//return result text
return result;
},
updateScore: (correct)=>{
if(correct) {quizData.score++;}
if(correct==0) {quizData.score = 0;}
return quizData.score;
},
};
let UIController = (function() {
let DOMstring ={
type: document.getElementById('types'),
score: document.querySelector('.score__text'),
result: document.querySelector('.result'),
question: document.querySelector('.question__text'),
choices: document.querySelector('.choices'),
newGame: document.querySelector('.newGame'),
clearUI:document.querySelectorAll('.question__text','.result')
}
return{
clearUI: function(){
DOMstring.question.textContent = "";
DOMstring.result.textContent = "";
DOMstring.choices.innerHTML = "";
if(quizData.score ==0){DOMstring.score.textContent = "Current Score : 0";}
},
displayNextQuiz: (type) => {
if(quizData.playingQuizArr.length > 0){
let n = Math.floor(Math.random()*quizData.playingQuizArr.length);
//display question
let q = quizData.type[type][quizData.playingQuizArr[n]];
DOMstring.question.textContent = q.question;
//display image(optional)
if(q.img){
let newDiv = document.createElement('div');
newDiv.className = "image";
newDiv.style.backgroundImage = "url(" + q.img + ")";
DOMstring.question.appendChild(newDiv);
}
//display choices
for(let i=0; i < q.choices.length;i++ )
{
let text = q.choices[i];
let btn = document.createElement("BUTTON");
btn.id = i;
btn.innerHTML = text;
btn.className= "btn-choice";
btn.addEventListener("click",function() {
let result = quizData.getAnswer(q.corectIdx,i,n);
if(result){
DOMstring.result.textContent = "Correct!";
btn.style.backgroundColor = "#98BF64";
}
else{
DOMstring.result.textContent = "Incorrect!";
btn.style.backgroundColor = "#CD5C5C";
}
DOMstring.score.textContent = "Current Score : " + quizData.score;
setTimeout(function(){
UIController.clearUI();
UIController.displayNextQuiz(type);
}
,400)
});
DOMstring.choices.appendChild(btn);
}
}
else{
DOMstring.result.textContent = "Thank you for completing the quiz.";
DOMstring.score.textContent = "Total Score : " + quizData.score;
}
},
getDOMString: () => {
return DOMstring;
},
addQuizTypes: () => {
DOMstring.type.options.length = 0;
let types = quizData.quizType();
types.forEach(el => {
let option = document.createElement("option");
option.value = el;
option.text = el;
DOMstring.type.add(option);
});
}
}
})();
let globalController = (function(UIController){
let DOMstring = UIController.getDOMString();
let eventListeners = () =>{
DOMstring.newGame.addEventListener('click', globalController.init);
DOMstring.type.addEventListener('change', function(){
quizData.resetGame(); UIController.clearUI();
let type = quizData.quizType()[DOMstring.type.selectedIndex];
quizData.generateNewQuizArr(type);
UIController.displayNextQuiz(type);
eventListeners();
});
};
return{
init: () =>{
quizData.resetGame();
UIController.clearUI();
UIController.addQuizTypes();
let type = quizData.quizType()[DOMstring.type.selectedIndex];
quizData.generateNewQuizArr(type);
UIController.displayNextQuiz(type);
eventListeners();
}
}
})(UIController);
globalController.init();