-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
450 lines (385 loc) Β· 14.1 KB
/
main.js
File metadata and controls
450 lines (385 loc) Β· 14.1 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
document.addEventListener("DOMContentLoaded", function () {
const loginSection = document.querySelector(".log_in");
const quizSection = document.querySelector(".quiz_interface");
const congratzSection = document.querySelector("#congratz");
const usernameInput = document.querySelector(".username");
const startButton = document.querySelector(".login_button");
const nameSpan = document.querySelector("#name");
const progressBar = document.querySelector(".step_progress");
const questionDiv = document.querySelector(".question");
const questionNumberSpan = document.querySelector("#qno");
const questionTextSpan = document.querySelector("#ques");
const options = document.querySelectorAll(".option");
const optionDiv = document.querySelector(".options");
const errorMessageBox = document.querySelector("#error_msg");
const submitButton = document.querySelector("#submit");
const congratzMsg = document.querySelector("#congratz_msg");
const congratzUsername = document.querySelector("#c_username");
const finalScore = document.querySelector("#final_score");
const retakeButton = document.querySelector("#retake");
const exitButton = document.querySelector("#exit");
const creatorBlock = document.querySelector("#creatorDiv-quiz_interface");
const logoutLink = document.querySelector(".logout");
const timerElement = document.querySelector(".timer");
let totalTime = 180; // 3 minutes in seconds
let timerInterval;
document.getElementById("timeUpModal").style.display = "none";
function updateTimerDisplay() {
const minutes = String(Math.floor(totalTime / 60)).padStart(2, "0");
const seconds = String(totalTime % 60).padStart(2, "0");
timerElement.innerHTML = `<span id="minutes">${minutes}</span>:<span id="seconds">${seconds}</span>`;
}
function startTimer() {
timerInterval = setInterval(function () {
if (totalTime > 0) {
totalTime--;
updateTimerDisplay();
} else {
clearInterval(timerInterval);
// Hide the step progress bar
progressBar.style.display = "none";
// Hide the questions
questionDiv.style.display = "none";
// Hide the options
optionDiv.style.display = "none";
// Hide the submit button
submitButton.style.display = "none";
// Hide the creator block
creatorBlock.style.display = "none";
// Show the time-up modal
document.getElementById("timeUpModal").style.display = "block";
document.getElementsByClassName("modal").style.display = "block";
}
}, 1000); // Update every second
}
// Handle the "OK" button click to close the modal
document.getElementById("closeModal").addEventListener("click", function () {
document.getElementById("timeUpModal").style.display = "none";
showResult(); // to display the score after click on "ok" in the dialog box
});
logoutLink.addEventListener("click", logout);
// Function to handle the logout action
function logout() {
// Clear the username input field
usernameInput.value = "";
currentQuestionIndex = 0;
score = 0;
// Clear the previous interval, if any
clearInterval(timerInterval);
totalTime = 183; // Reset the timer to 3 minutes in seconds(because 3 sec difference after logout)
updateTimerDisplay(); // Update the timer display
// Reset step progress bar
const progressSteps = document.querySelectorAll(".progress-step");
progressSteps.forEach((step) => {
step.classList.remove("correct-step", "wrong-step");
});
// Reset options
options.forEach((option) => {
option.classList.remove("selected", "correct", "wrong", "hover");
});
// Hide the quiz and congratz sections
quizSection.style.display = "none";
congratzSection.style.display = "none";
// Show the login section
loginSection.style.display = "block";
// Start the timer again
startTimer();
}
let currentQuestionIndex = 0;
let score = 0;
const quizData = [
{
question: 'Which of the following words is a synonym for "ubiquitous"?',
options: ["Scarce", "Abundant", "Pervasive", "Sporadic"],
correctAnswer: 2,
},
{
question:
"Which programming language is known for its use in artificial intelligence and data science?",
options: ["Java", "Python", "C++", "Ruby"],
correctAnswer: 1,
},
{
question:
"If all cats are mammals, and Fluffy is a cat, what can you conclude?",
options: [
"Fluffy is not a mammal.",
"Fluffy is a mammal.",
"All mammals are cats.",
"Some mammals are cats.",
],
correctAnswer: 1,
},
{
question: "Who led the Bolshevik Revolution in Russia in 1917?",
options: [
"Nicholas II",
"Joseph Stalin",
"Leon Trotsky",
"Vladimir Lenin",
],
correctAnswer: 3,
},
{
question:
"Which data structure follows the Last-In-First-Out (LIFO) principle?",
options: ["Queue", "Stack", "Linked List", "Array"],
correctAnswer: 1,
},
{
question: 'What is the opposite of "expand"?',
options: ["Shrink", "Increase", "Stretch", "Enlarge"],
correctAnswer: 0,
},
{
question:
"What is the phenomenon where a wave changes direction as it passes from one medium to another?",
options: ["Diffraction", "Reflection", "Refraction", "Dispersion"],
correctAnswer: 2,
},
{
question:
"How many degrees are in the sum of the interior angles of a hexagon?",
options: ["120 degrees", "180 degrees", "90 degrees", "720 degrees"],
correctAnswer: 3,
},
{
question: 'Who painted the famous artwork "Starry Night"?',
options: [
"Vincent van Gogh",
"Leonardo da Vinci",
"Pablo Picasso",
"Michelangelo",
],
correctAnswer: 0,
},
{
question:
"In a logic puzzle, if Ann is taller than Bob, and Bob is shorter than Carla, who is the tallest?",
options: ["Ann", "Bob", "Carla", "Not enough information"],
correctAnswer: 2,
},
];
// Hide quiz and congratz sections initially
quizSection.style.display = "none";
congratzSection.style.display = "none";
startButton.addEventListener("click", startQuiz);
function startQuiz() {
// Clear the previous interval, if any
clearInterval(timerInterval);
// Call startTimer to begin the countdown
startTimer();
const username = usernameInput.value;
if (username.trim() === "") {
// Display an alert message if the username is empty
alert("Please enter a Username");
return;
}
// Hide the login section and show the quiz section
loginSection.style.display = "none";
quizSection.style.display = "block";
// Initialize the quiz
currentQuestionIndex = 0;
score = 0;
// Update the username displayed in the quiz section
nameSpan.textContent = `${"Welcome! " + username}`;
// Display the first question
showQuestion(currentQuestionIndex);
}
function showQuestion(index) {
const question = quizData[index];
questionNumberSpan.textContent = `${index + 1 + ". "}`;
questionTextSpan.textContent = question.question;
for (let i = 0; i < options.length; i++) {
options[i].textContent = question.options[i];
options[i].classList.remove("selected", "correct", "wrong", "hover");
}
// Handle option click events
options.forEach((option, i) => {
option.addEventListener("click", () => {
const selectedAnswer = i;
options.forEach((opt, j) => opt.classList.remove("selected"));
option.classList.add("selected");
// Disable the 'Submit' button until an option is selected
submitButton.disabled = false;
});
});
}
errorMessageBox.style.display = "none";
// Add event listener for the "Submit" button
submitButton.addEventListener("click", () => {
const selectedAnswer = getSelectedOption();
if (selectedAnswer === undefined) {
// Display the error message
errorMessageBox.style.display = "block";
return;
} else {
// Hide the error message
errorMessageBox.style.display = "none";
}
const correctAnswer = quizData[currentQuestionIndex].correctAnswer;
if (selectedAnswer === correctAnswer) {
options[selectedAnswer].classList.remove("selected");
// Highlight the correct option in green
options[correctAnswer].classList.add("correct");
score++;
// Add a class to the corresponding step progress bar item for a correct answer
const progressStep = document.querySelector(
`#qn${currentQuestionIndex + 1}`
);
progressStep.classList.add("correct-step");
} else {
// Highlight the selected option in red if it is wrong
options[selectedAnswer].classList.add("wrong");
options[selectedAnswer].classList.remove("selected");
// Highlight the selected option in green if it is correct
options[correctAnswer].classList.add("correct");
// Add a class to the corresponding step progress bar item for a wrong answer
const progressStep = document.querySelector(
`#qn${currentQuestionIndex + 1}`
);
progressStep.classList.add("wrong-step");
}
// Disable all options
options.forEach((option) => option.classList.add("hover"));
submitButton.disabled = true;
// Delay moving to the next question to allow time for feedback
setTimeout(() => {
nextQuestion();
}, 1200);
});
function nextQuestion() {
currentQuestionIndex++;
if (currentQuestionIndex < quizData.length) {
// Display the next question
showQuestion(currentQuestionIndex);
submitButton.disabled = false; // Re-enable the "Submit" button for the next question
} else {
// End of the quiz
showResult();
}
}
function showResult() {
clearInterval(timerInterval); // Stop the timer
quizSection.style.display = "none";
congratzSection.style.display = "block";
let message = ""; // Initialize an empty message
// Determine the message based on the score
if (score === 10) {
message = "Champion!!!π";
} else if (score === 9) {
message = "Master!!!π";
} else if (score === 8) {
message = "Smart!!!π";
} else if (score === 7) {
message = "Well done!!!π";
} else if (score === 6) {
message = "Good!!!π";
} else {
message = "Try harder!!!π";
}
if (currentQuestionIndex < quizData.length) {
//const percentageScore = (score / quizData.length) * 100;
//finalScore.textContent = percentageScore.toFixed(2) + "%";
// Last question, show the user's score
finalScore.textContent = score + " out of " + quizData.length;
// Display the special message based on score
finalScore.insertAdjacentHTML(
"afterend",
`<p id="scoreMessage">${message}</p>`
);
} else {
// Display the special message based on score
finalScore.insertAdjacentHTML(
"afterend",
`<p id="scoreMessage">${message}</p>`
);
// Last question, show the user's score
finalScore.textContent = score + " out of " + quizData.length;
}
congratzMsg.textContent = "Quiz Completed...";
congratzUsername.textContent = `${usernameInput.value}!!!`;
}
retakeButton.addEventListener("click", restartQuiz);
exitButton.addEventListener("click", exitQuiz);
function restartQuiz() {
currentQuestionIndex = 0;
score = 0;
// Clear the previous interval, if any
clearInterval(timerInterval);
totalTime = 180; // Reset the timer to 3 minutes in seconds
updateTimerDisplay(); // Update the timer display
progressBar.style.display = "block";
questionDiv.style.display = "block";
optionDiv.style.display = "flex";
optionDiv.classList.add("options-responsive");
submitButton.style.display = "block";
creatorBlock.style.display = "block";
// Reset step progress bar
const progressSteps = document.querySelectorAll(".progress-step");
progressSteps.forEach((step) => {
step.classList.remove("correct-step", "wrong-step");
});
// Reset options
options.forEach((option) => {
option.classList.remove("selected", "correct", "wrong", "hover");
});
// Clear the final score
finalScore.textContent = "";
const scoreMessage = document.getElementById("scoreMessage");
if (scoreMessage) {
scoreMessage.remove();
}
// Show the first question
showQuestion(currentQuestionIndex);
congratzSection.style.display = "none";
loginSection.style.display = "none";
quizSection.style.display = "block";
// Start the timer again
startTimer();
}
function exitQuiz() {
currentQuestionIndex = 0;
score = 0;
// Clear the previous interval, if any
clearInterval(timerInterval);
totalTime = 183; // Reset the timer to 3 minutes in seconds
updateTimerDisplay(); // Update the timer display
progressBar.style.display = "block";
questionDiv.style.display = "block";
optionDiv.style.display = "flex";
optionDiv.classList.add("options-responsive");
submitButton.style.display = "block";
creatorBlock.style.display = "block";
// Reset step progress bar
const progressSteps = document.querySelectorAll(".progress-step");
progressSteps.forEach((step) => {
step.classList.remove("correct-step", "wrong-step");
});
// Reset options
options.forEach((option) => {
option.classList.remove("selected", "correct", "wrong", "hover");
});
// Clear the final score
finalScore.textContent = "";
//clear the username
nameSpan.textContent = "";
const scoreMessage = document.getElementById("scoreMessage");
if (scoreMessage) {
scoreMessage.remove();
}
congratzSection.style.display = "none";
loginSection.style.display = "block";
usernameInput.value = "";
// Start the timer again
startTimer();
}
function getSelectedOption() {
for (let i = 0; i < options.length; i++) {
if (options[i].classList.contains("selected")) {
return i;
}
}
return undefined;
}
});