-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
337 lines (320 loc) · 7.41 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
const questions = [
{
question: "What does HTML stand for?",
answers: [
{
text: "Hyper Text Markup Language",
correct: true,
},
{
text: "High Text Machine Language",
correct: false,
},
{
text: "Hyper Text Machine Learning",
correct: false,
},
{
text: "Hyper Text Marking Language",
correct: false,
},
],
},
{
question: "What is the purpose of the `alt` attribute in an `<img>` tag?",
answers: [
{
text: "To specify the URL of the image",
correct: false,
},
{
text: "To provide alternative text for the image",
correct: true,
},
{
text: "To align the image",
correct: false,
},
{
text: "To style the image with CSS",
correct: false,
},
],
},
{
question: "Which CSS property is used to change the text color?",
answers: [
{
text: "color",
correct: true,
},
{
text: "font-color",
correct: false,
},
{
text: "text-color",
correct: false,
},
{
text: "background-color",
correct: false,
},
],
},
{
question: "Which is the correct JavaScript syntax to write 'Hello World'?",
answers: [
{
text: "print('Hello World');",
correct: false,
},
{
text: "document.write('Hello World');",
correct: true,
},
{
text: "echo('Hello World');",
correct: false,
},
{
text: "console.print('Hello World');",
correct: false,
},
],
},
{
question:
"Which of the following is a valid way to add comments in JavaScript?",
answers: [
{
text: "// This is a comment",
correct: true,
},
{
text: "/* This is a comment */",
correct: true,
},
{
text: "# This is a comment",
correct: false,
},
{
text: "!-- This is a comment --",
correct: false,
},
],
},
{
question: "Which CSS property controls the layout of elements?",
answers: [
{
text: "display",
correct: true,
},
{
text: "position",
correct: false,
},
{
text: "float",
correct: false,
},
{
text: "visibility",
correct: false,
},
],
},
{
question: "How do you declare a JavaScript variable?",
answers: [
{
text: "let myVariable;",
correct: true,
},
{
text: "var = myVariable;",
correct: false,
},
{
text: "variable myVariable;",
correct: false,
},
{
text: "myVariable: var;",
correct: false,
},
],
},
{
question: "What is the purpose of the <head> tag in HTML?",
answers: [
{
text: "To contain meta information about the document",
correct: true,
},
{
text: "To display the title of the page",
correct: false,
},
{
text: "To include the main content of the page",
correct: false,
},
{
text: "To declare the doctype",
correct: false,
},
],
},
{
question: "Which JavaScript method is used to find an HTML element by ID?",
answers: [
{
text: "document.getElementById",
correct: true,
},
{
text: "document.getElementsById",
correct: false,
},
{
text: "document.querySelector",
correct: false,
},
{
text: "document.getElement",
correct: false,
},
],
},
{
question:
"Which CSS property is used to create space inside an element, between the content and the border?",
answers: [
{
text: "padding",
correct: true,
},
{
text: "margin",
correct: false,
},
{
text: "border-spacing",
correct: false,
},
{
text: "spacing",
correct: false,
},
],
},
];
const questionElement = document.getElementById("question");
const answerButton = document.getElementById("answer-button");
const nextButton = document.getElementById("next-btn");
let currentQuestionIndex = 0;
let score = 0;
let timerElement = document.getElementById("timer");
let timeLeft = 10; // Set the time limit for each question
let timerInterval; // To store the interval ID
function startTimer() {
timeLeft = 10; // Reset the time for each question
timerElement.innerHTML = `Time Left: ${timeLeft}s`;
timerInterval = setInterval(() => {
timeLeft--;
timerElement.innerHTML = `Time Left: ${timeLeft}s`;
if (timeLeft <= 0) {
clearInterval(timerInterval); // Stop the timer
handleTimeout(); // Handle when time runs out
}
}, 1000); // Update every second
}
function stopTimer() {
clearInterval(timerInterval); // Stop the timer
}
function handleTimeout() {
// Disable all buttons and show the correct answer
Array.from(answerButton.children).forEach((button) => {
if (button.dataset.correct === "true") {
button.classList.add("correct");
}
button.disabled = true;
});
nextButton.style.display = "block"; // Show the Next button
}
function startQuiz() {
currentQuestionIndex = 0;
score = 0;
timerElement.style.display = "block"; // Show the timer
nextButton.innerHTML = "Next";
showQuestion();
}
function showQuestion() {
resetSate();
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");
answerButton.appendChild(button);
if (answer.correct) {
button.dataset.correct = answer.correct;
}
button.addEventListener("click", (e) => {
stopTimer(); // Stop the timer when an answer is selected
selectAnswer(e);
});
});
startTimer(); // Start the timer for the current question
}
function resetSate() {
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((button) => {
if (button.dataset.correct === "true") {
button.classList.add("correct");
}
button.disabled = true;
});
nextButton.style.display = "block";
}
function showScore() {
resetSate();
timerElement.style.display = "none"; // Hide the timer when the quiz ends
questionElement.innerHTML = `Your 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();