-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
295 lines (284 loc) · 8.79 KB
/
script.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
const questions = [
{
question: "What is the difference between innerHTML and textContent?",
answers: [
{
text: "innerHTML includes the HTML tags, while textContent does not.",
correct: true,
},
{
text: "innerHTML can be used to modify the DOM, while textContent cannot.",
correct: false,
},
{
text: "innerHTML is a global property, while textContent is a custom property.",
correct: false,
},
{
text: "All of the above",
correct: false,
},
],
},
{
question: "What is the purpose of the prototype property in JavaScript?",
answers: [
{
text: "To modify an object",
correct: false,
},
{
text: "To inherit properties from another object",
correct: false,
},
{
text: "To create a new object",
correct: false,
},
{
text: "To store the prototype of an object",
correct: true,
},
],
},
{
question: "What is the difference between a constructor function and a regular function in JavaScript?",
answers: [
{
text: "A constructor function cannot be called directly, while a regular function can.",
correct: false,
},
{
text: "A constructor function has a prototype property, while a regular function does not.",
correct: false,
},
{
text: " A constructor function is used to create new objects, while a regular function is used to perform an action.",
correct: true,
},
{
text: "All the above",
correct: false,
},
],
},
{
question: "What is the purpose of the this keyword in JavaScript?",
answers: [
{
text: "To refer to the global object.",
correct: false,
},
{
text: "To refer to the current object.",
correct: true,
},
{
text: "To refer to the window object.",
correct: false,
},
{
text: "All the above",
correct: false,
},
],
},
{
question: "What is the difference between === and == in JavaScript?",
answers: [
{
text: "=== checks for strict equality, while == checks for loose equality.",
correct: true,
},
{
text: "=== can be used with objects, while == cannot.",
correct: false,
},
{
text: "=== is a newer operator than ==.",
correct: false,
},
{
text: "All the above",
correct: false,
},
],
},
{
question: "What is the purpose of the typeof operator in JavaScript?",
answers: [
{
text: "To get the length of a variable.",
correct: false,
},
{
text: "To get the value of a variable.",
correct: false,
},
{
text: "To get the type of a variable.",
correct: true,
},
{
text: "To get the name of a variable.",
correct: false,
},
],
},
{
question: "What is the difference between NaN and undefined in JavaScript?`;",
answers: [
{
text: "NaN is a global constant, while undefined is a global variable.",
correct: false,
},
{
text: "NaN is a special value that represents a non-number, while undefined is a special value that represents a variable that has not been assigned a value.",
correct: true,
},
{
text: "NaN can be used in mathematical operations, while undefined cannot.",
correct: false,
},
{
text: "All the above",
correct: false,
},
],
},
{
question: "What is the purpose of the isNaN() function in JavaScript?",
answers: [
{
text: "To check if a value is a string.",
correct: false,
},
{
text: "To check if a value is an object.",
correct: false,
},
{
text: "To check if a value is undefined.",
correct: false,
},
{
text: "To check if a value is a number.",
correct: true,
},
],
},
{
question: "What is the purpose of the setTimeout() function in JavaScript?",
answers: [
{
text: "To delay the execution of a function.",
correct: true,
},
{
text: "To cancel the execution of a function.",
correct: false,
},
{
text: "To repeat the execution of a function.",
correct: false,
},
{
text: "To stop the execution of a function.",
correct: false,
},
],
},
{
question: "What is the purpose of the addEventListener() method in JavaScript?",
answers: [
{
text: "To add an event listener to an element.",
correct: true,
},
{
text: "To remove an event listener from an element.",
correct: false,
},
{
text: "To modify an event listener on an element.",
correct: false,
},
{
text: "To listen for events on an element.",
correct: false,
},
],
},
];
const questionElement = document.getElementById("question");
const answerButtons = document.getElementById("answer-buttons");
const nextButton = document.getElementById("next-btn");
let currentQuestionIndex = 0;
let score = 0;
function startQuiz(){
currentQuestionIndex = 0;
score = 0;
nextButton.innerHTML = "Next";
showQuestion();
}
function showQuestion(){
resetState();
let currentQuestion = questions[currentQuestionIndex];
let questionNo = currentQuestionIndex + 1;
questionElement.innerHTML = questionNo + "." + currentQuestion.question;
currentQuestion.answers.forEach(answer => {
const button = document.createElement("button");
button.innerHTML = answer.text;
button.classList.add("btn");
answerButtons.appendChild(button);
if(answer.correct){
button.dataset.correct = answer.correct;
}
button.addEventListener("click", selectAnswer);
});
}
function resetState(){
nextButton.style.display ="none";
while(answerButtons.firstChild){
answerButtons.removeChild(answerButtons.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(answerButtons.children).forEach(button => {
if(button.dataset.correct === "true"){
button.classList.add("correct");
}
button.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();