-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
309 lines (301 loc) · 9.25 KB
/
app.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
306
307
308
309
const questions = [
{
question: "Which is the national animal of India?",
answer: [
{ text: "lion", correct: false },
{ text: "tiger", correct: true },
{ text: "elephant", correct: false },
{ text: "monkey", correct: false },
],
},
{
question: "What is the capital city of France?",
answer: [
{ text: "Paris", correct: true },
{ text: "London", correct: false },
{ text: "Berlin", correct: false },
{ text: "Rome", correct: false },
],
},
{
question: "Which planet is known as the Red Planet?",
answer: [
{ text: "Venus", correct: false },
{ text: "Mars", correct: true },
{ text: "Jupiter", correct: false },
{ text: "Saturn", correct: false },
],
},
{
question: "Who wrote 'Romeo and Juliet'?",
answer: [
{ text: "Charles Dickens", correct: false },
{ text: "William Shakespeare", correct: true },
{ text: "Jane Austen", correct: false },
{ text: "Mark Twain", correct: false },
],
},
{
question: "What is the largest mammal in the world?",
answer: [
{ text: "Elephant", correct: false },
{ text: "Blue Whale", correct: true },
{ text: "Giraffe", correct: false },
{ text: "Hippopotamus", correct: false },
],
},
{
question: "Which is the largest ocean on Earth?",
answer: [
{ text: "Atlantic Ocean", correct: false },
{ text: "Indian Ocean", correct: false },
{ text: "Southern Ocean", correct: false },
{ text: "Pacific Ocean", correct: true },
],
},
{
question: "What is the capital city of France?",
answer: [
{ text: "Paris", correct: true },
{ text: "London", correct: false },
{ text: "Berlin", correct: false },
{ text: "Rome", correct: false },
],
},
{
question: "Which planet is known as the Red Planet?",
answer: [
{ text: "Venus", correct: false },
{ text: "Mars", correct: true },
{ text: "Jupiter", correct: false },
{ text: "Saturn", correct: false },
],
},
{
question: "Who painted the Mona Lisa?",
answer: [
{ text: "Pablo Picasso", correct: false },
{ text: "Leonardo da Vinci", correct: true },
{ text: "Vincent van Gogh", correct: false },
{ text: "Michelangelo", correct: false },
],
},
{
question: "What is the currency of Japan?",
answer: [
{ text: "Yuan", correct: false },
{ text: "Pound", correct: false },
{ text: "Yen", correct: true },
{ text: "Euro", correct: false },
],
},
{
question: "What is the largest mammal in the world?",
answer: [
{ text: "Elephant", correct: false },
{ text: "Blue Whale", correct: true },
{ text: "Giraffe", correct: false },
{ text: "Hippopotamus", correct: false },
],
},
{
question: "In which year did World War II end?",
answer: [
{ text: "1943", correct: false },
{ text: "1945", correct: true },
{ text: "1947", correct: false },
{ text: "1950", correct: false },
],
},
{
question: "Who is the author of 'To Kill a Mockingbird'?",
answer: [
{ text: "George Orwell", correct: false },
{ text: "J.K. Rowling", correct: false },
{ text: "Harper Lee", correct: true },
{ text: "Ernest Hemingway", correct: false },
],
},
{
question: "What is the largest desert in the world?",
answer: [
{ text: "Sahara Desert", correct: false },
{ text: "Arctic Desert", correct: true },
{ text: "Gobi Desert", correct: false },
{ text: "Karakum Desert", correct: false },
],
},
{
question: "Who developed the theory of relativity?",
answer: [
{ text: "Isaac Newton", correct: false },
{ text: "Albert Einstein", correct: true },
{ text: "Galileo Galilei", correct: false },
{ text: "Stephen Hawking", correct: false },
],
},
{
question: "What is the smallest prime number?",
answer: [
{ text: "1", correct: false },
{ text: "2", correct: true },
{ text: "3", correct: false },
{ text: "5", correct: false },
],
},
{
question: "Who painted 'Starry Night'?",
answer: [
{ text: "Pablo Picasso", correct: false },
{ text: "Vincent van Gogh", correct: true },
{ text: "Claude Monet", correct: false },
{ text: "Salvador Dali", correct: false },
],
},
{
question: "What is the capital of Japan?",
answer: [
{ text: "Seoul", correct: false },
{ text: "Beijing", correct: false },
{ text: "Tokyo", correct: true },
{ text: "Osaka", correct: false },
],
},
{
question: "Which element has the chemical symbol 'O'?",
answer: [
{ text: "Osmium", correct: false },
{ text: "Oxygen", correct: true },
{ text: "Oganesson", correct: false },
{ text: "Opium", correct: false },
],
},
{
question: "Who wrote 'The Great Gatsby'?",
answer: [
{ text: "F. Scott Fitzgerald", correct: true },
{ text: "Jane Austen", correct: false },
{ text: "George Orwell", correct: false },
{ text: "Charles Dickens", correct: false },
],
},
{
question: "What is the largest continent?",
answer: [
{ text: "North America", correct: false },
{ text: "Asia", correct: true },
{ text: "Africa", correct: false },
{ text: "Europe", correct: false },
],
},
{
question: "Who is known as the 'Father of Computer Science'?",
answer: [
{ text: "Alan Turing", correct: true },
{ text: "Bill Gates", correct: false },
{ text: "Steve Jobs", correct: false },
{ text: "Charles Babbage", correct: false },
],
},
{
question: "What is the currency of Brazil?",
answer: [
{ text: "Peso", correct: false },
{ text: "Real", correct: true },
{ text: "Dollar", correct: false },
{ text: "Euro", correct: false },
],
},
{
question: "Which is the smallest planet in our solar system?",
answer: [
{ text: "Mercury", correct: true },
{ text: "Mars", correct: false },
{ text: "Venus", correct: false },
{ text: "Pluto", correct: false },
],
},
{
question: "What is the capital of Australia?",
answer: [
{ text: "Sydney", correct: false },
{ text: "Melbourne", correct: false },
{ text: "Canberra", correct: true },
{ text: "Brisbane", correct: false },
],
},
];
const questionElement = document.getElementById("question");
const answerButton = document.getElementById("answer-buttons");
const nextButton = document.getElementById("next-btn");
let currentQuestionIndex = 0;
let score = 0;
function startQuiz() {
showQuestion();
currentQuestionIndex = 0;
score = 0;
nextButton.innerHTML = "Next";
}
function showQuestion() {
resetState();
let currentQuestion = questions[currentQuestionIndex];
let questionNo = currentQuestionIndex + 1;
questionElement.innerHTML = questionNo + ". " + currentQuestion.question;
currentQuestion.answer.forEach((answer) => {
const buttons = document.createElement("button");
buttons.innerHTML=answer.text;
buttons.classList.add('btn');
answerButton .appendChild (buttons);
if(answer.correct){
buttons.dataset.correct=answer.correct;
}
buttons.addEventListener('click',selectAnswer)
});
}
function resetState(){
nextButton.style.display='none';
while(answerButton.firstChild){
answerButton.removeChild(answerButton.firstChild);
}
}
function selectAnswer(e){
const selectedBtn = e.target;
const isCorrect = selectedBtn.dataset.correct==='true';
if(isCorrect){
selectedBtn.classList.add('correct');
score++;
}
else{
selectedBtn.classList.add('incorrect');
}
Array.from(answerButton.children).forEach(buttons=>{
if(buttons.dataset.correct==='true'){
buttons.classList.add('correct');
}
buttons.disabled=true;
});
nextButton.style.display='block';
}
function showScore(){
resetState();
questionElement.innerHTML=`You scored ${score} out of ${questions.length} !`;
nextButton.innerHTML='Play Again';
nextButton.style.display='block';
}
function handleNextButton(){
currentQuestionIndex++;
if(currentQuestionIndex < questions.length){
showQuestion();
}else{
showScore();
}
}
nextButton.addEventListener('click',()=>{
if(currentQuestionIndex < questions.length){
handleNextButton();
}
else{
startQuiz();
}
})
startQuiz();