-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
424 lines (388 loc) · 16.8 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
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
let questions = [
{
numb: 1,
question: "What does HTML stand for?",
answer: "Hyper Text Markup Language",
options: [
"Hyper Text Preprocessor",
"Hyper Text Markup Language",
"Hyper Text Multiple Language",
"Hyper Tool Multi Language"
]
},
{
numb: 2,
question: "What does CSS stand for?",
answer: "Cascading Style Sheet",
options: [
"Common Style Sheet",
"Colorful Style Sheet",
"Computer Style Sheet",
"Cascading Style Sheet"
]
},
{
numb: 3,
question: "What does PHP stand for?",
answer: "Hypertext Preprocessor",
options: [
"Hypertext Preprocessor",
"Hypertext Programming",
"Hypertext Preprogramming",
"Hometext Preprocessor"
]
},
{
numb: 4,
question: "What does SQL stand for?",
answer: "Structured Query Language",
options: [
"Stylish Question Language",
"Stylesheet Query Language",
"Statement Question Language",
"Structured Query Language"
]
},
{
numb: 5,
question: "What does XML stand for?",
answer: "eXtensible Markup Language",
options: [
"eXtensible Markup Language",
"eXecutable Multiple Language",
"eXTra Multi-Program Language",
"eXamine Multiple Language"
]
},
// you can uncomment the below codes and make duplicate as more as you want to add question
// but remember you need to give the numb value serialize like 1,2,3,5,6,7,8,9.....
// {
// {
// numb: 6,
// question: "How many players are there in one team of cricket ?",
// answer: "11",
// options: [
// "13",
// "12",
// "11",
// "none of the above"
// ]
// },
// {
// numb: 7,
// question: "A cricket match is divided into periods called ________",
// answer: "innings",
// options: [
// "halfs",
// "intervals",
// "innings",
// "none of the above"
// ]
// },
// {
// numb: 8,
// question: "what is lenght of pitch",
// answer: "20.5 m",
// options: [
// "20.4 m",
// "20.5 m",
// "22 m",
// "none of the above"
// ]
// },
// {
// numb: 9,
// question: "The first official international match of cricket was held in 1844 between …",
// answer: "The United States and Canada",
// options: [
// "India and Afghanistan",
// " England and Australia",
// "The United States and Canada",
// "India Vs Pakistan"
// ]
// },
// {
// numb: 10,
// question: "The highest score of Sachin Tendulkar in his international cricket career was …",
// answer: "248",
// options: [
// "248",
// "200",
// "209",
// "none of the above"
// ]
// },
];
const start_btn = document.querySelector(".start_btn button");
const info_box = document.querySelector(".info_box");
const exit_btn = info_box.querySelector(".buttons .quit");
const continue_btn = info_box.querySelector(".buttons .restart");
const quiz_box = document.querySelector(".quiz_box");
const result_box = document.querySelector(".result_box");
const option_list = document.querySelector(".option_list");
const time_line = document.querySelector("header .time_line");
const timeText = document.querySelector(".timer .time_left_txt");
const timeCount = document.querySelector(".timer .timer_sec");
// if startQuiz button clicked
start_btn.onclick = ()=>{
info_box.classList.add("activeInfo"); //show info box
}
// if exitQuiz button clicked
exit_btn.onclick = ()=>{
info_box.classList.remove("activeInfo"); //hide info box
}
// if continueQuiz button clicked
continue_btn.onclick = ()=>{
info_box.classList.remove("activeInfo"); //hide info box
quiz_box.classList.add("activeQuiz"); //show quiz box
showQuetions(0); //calling showQestions function
queCounter(1); //passing 1 parameter to queCounter
startTimer(60); //calling startTimer function
startTimerLine(0); //calling startTimerLine function
}
let timeValue = 60;
let que_count = 0;
let que_numb = 1;
let userScore = 0;
let counter;
let counterLine;
let widthValue = 0;
const restart_quiz = result_box.querySelector(".buttons .restart");
const quit_quiz = result_box.querySelector(".buttons .quit");
// if restartQuiz button clicked
restart_quiz.onclick = ()=>{
quiz_box.classList.add("activeQuiz"); //show quiz box
result_box.classList.remove("activeResult"); //hide result box
timeValue = 60;
que_count = 0;
que_numb = 1;
userScore = 0;
widthValue = 0;
showQuetions(que_count); //calling showQestions function
queCounter(que_numb); //passing que_numb value to queCounter
clearInterval(counter); //clear counter
clearInterval(counterLine); //clear counterLine
startTimer(timeValue); //calling startTimer function
startTimerLine(widthValue); //calling startTimerLine function
timeText.textContent = "Time Left"; //change the text of timeText to Time Left
next_btn.classList.remove("show"); //hide the next button
}
// if quitQuiz button clicked
quit_quiz.onclick = ()=>{
window.location.reload(); //reload the current window
}
const next_btn = document.querySelector("footer .next_btn");
const bottom_ques_counter = document.querySelector("footer .total_que");
// if Next Que button clicked
next_btn.onclick = ()=>{
if(que_count < questions.length - 1){ //if question count is less than total question length
que_count++; //increment the que_count value
que_numb++; //increment the que_numb value
showQuetions(que_count); //calling showQestions function
queCounter(que_numb); //passing que_numb value to queCounter
clearInterval(counter); //clear counter
clearInterval(counterLine); //clear counterLine
startTimer(timeValue); //calling startTimer function
startTimerLine(widthValue); //calling startTimerLine function
timeText.textContent = "Time Left"; //change the timeText to Time Left
next_btn.classList.remove("show"); //hide the next button
}else{
clearInterval(counter); //clear counter
clearInterval(counterLine); //clear counterLine
showResult(); //calling showResult function
}
}
// getting questions and options from array
function showQuetions(index){
const que_text = document.querySelector(".que_text");
//creating a new span and div tag for question and option and passing the value using array index
let que_tag = '<span>'+ questions[index].numb + ". " + questions[index].question +'</span>';
let option_tag = '<div class="option"><span>'+ questions[index].options[0] +'</span></div>'
+ '<div class="option"><span>'+ questions[index].options[1] +'</span></div>'
+ '<div class="option"><span>'+ questions[index].options[2] +'</span></div>'
+ '<div class="option"><span>'+ questions[index].options[3] +'</span></div>';
que_text.innerHTML = que_tag; //adding new span tag inside que_tag
option_list.innerHTML = option_tag; //adding new div tag inside option_tag
const option = option_list.querySelectorAll(".option");
// set onclick attribute to all available options
for(i=0; i < option.length; i++){
option[i].setAttribute("onclick", "optionSelected(this)");
}
}
// creating the new div tags which for icons
let tickIconTag = '<div class="icon tick"><i class="fas fa-check"></i></div>';
let crossIconTag = '<div class="icon cross"><i class="fas fa-times"></i></div>';
//if user clicked on option
function optionSelected(answer){
clearInterval(counter); //clear counter
clearInterval(counterLine); //clear counterLine
let userAns = answer.textContent; //getting user selected option
let correcAns = questions[que_count].answer; //getting correct answer from array
const allOptions = option_list.children.length; //getting all option items
if(userAns == correcAns){ //if user selected option is equal to array's correct answer
userScore += 1; //upgrading score value with 1
answer.classList.add("correct"); //adding green color to correct selected option
answer.insertAdjacentHTML("beforeend", tickIconTag); //adding tick icon to correct selected option
console.log("Correct Answer");
console.log("Your correct answers = " + userScore);
}else{
answer.classList.add("incorrect"); //adding red color to correct selected option
answer.insertAdjacentHTML("beforeend", crossIconTag); //adding cross icon to correct selected option
console.log("Wrong Answer");
for(i=0; i < allOptions; i++){
if(option_list.children[i].textContent == correcAns){ //if there is an option which is matched to an array answer
option_list.children[i].setAttribute("class", "option correct"); //adding green color to matched option
option_list.children[i].insertAdjacentHTML("beforeend", tickIconTag); //adding tick icon to matched option
console.log("Auto selected correct answer.");
}
}
}
for(i=0; i < allOptions; i++){
option_list.children[i].classList.add("disabled"); //once user select an option then disabled all options
}
next_btn.classList.add("show"); //show the next button if user selected any option
}////////
// Function to display questions and answers after the quiz
function showResult(){
info_box.classList.remove("activeInfo"); //hide info box
quiz_box.classList.remove("activeQuiz"); //hide quiz box
result_box.classList.add("activeResult"); //show result box
const scoreText = result_box.querySelector(".score_text");
// Initialize variables to count attempted questions and correct answers
let attemptedQuestions = 0;
//let correctAnswers = 0;
// Create a variable to store the HTML content for displaying questions, chosen answers, correct answers, and correctness
let userAnswersHTML = '';
// Loop through each question
questions.forEach((question, index) => {
// Increment attempted questions count
attemptedQuestions++;
// Add question text
userAnswersHTML += `<div class="user-answer"><span>${question.numb}. ${question.question}</span>`;
// Get the index of the chosen answer
const selectedOption = option_list.querySelector('.selected');
let userSelectedAnswer;
if (selectedOption) {
const selectedOptionText = selectedOption.textContent.trim();
const optionIndex = question.options.findIndex(option => option === selectedOptionText);
userSelectedAnswer = question.options[optionIndex];
}
// else {
// userSelectedAnswer = "No answer selected";
// }
// Add user's chosen answer
// userAnswersHTML += `<div class="chosen-answer"><span>Chosen Answer: ${userSelectedAnswer}</span></div>`;
// Add correct answer to HTML content
userAnswersHTML += `<div class="correct-answer"><span>Correct Answer: ${question.answer}</span></div>`;
// Check if the user's answer is correct
// const isCorrect = userSelectedAnswer === question.answer;
// if (isCorrect) {
// correctAnswers++;
// userAnswersHTML += `<div class="answer-correctness"><span>Your answer is incorrect!</span></div>`;
// }
// else {
// userAnswersHTML += `<div class="answer-correctness"><span>Your answer is correct!</span></div>`;
// }
userAnswersHTML += `</div>`; // Close user-answer div
let total_time=5;
let msg=document.querySelector(".complete_text");
msg.innerText=`You've completed the Quiz in ${total_time} min`;
});
// Display the HTML content
scoreText.innerHTML = userAnswersHTML;
// Display the number of attempted questions out of the total
const attemptSummary = document.createElement('div');
attemptSummary.innerHTML = `<p>Attempted ${attemptedQuestions} out of ${questions.length} questions.</p>`;
scoreText.appendChild(attemptSummary);
}
// function showResult(){
// info_box.classList.remove("activeInfo"); //hide info box
// quiz_box.classList.remove("activeQuiz"); //hide quiz box
// result_box.classList.add("activeResult"); //show result box
// const scoreText = result_box.querySelector(".score_text");
// if (userScore > 3){ // if user scored more than 3
// //creating a new span tag and passing the user score number and total question number
// let scoreTag = '<span>and congrats! , You got <p>'+ userScore +'</p> out of <p>'+ questions.length +'</p></span>';
// scoreText.innerHTML = scoreTag; //adding new span tag inside score_Text
// }
// else if(userScore > 1){ // if user scored more than 1
// let scoreTag = '<span>and nice , You got <p>'+ userScore +'</p> out of <p>'+ questions.length +'</p></span>';
// scoreText.innerHTML = scoreTag;
// }
// else{ // if user scored less than 1
// let scoreTag = '<span>and sorry , You got only <p>'+ userScore +'</p> out of <p>'+ questions.length +'</p></span>';
// scoreText.innerHTML = scoreTag;
// }
// quiz_box.classList.remove("activeQuiz");
// // Show the result box
// result_box.classList.add("activeResult");
// const scoreText = result_box.querySelector(".score_text");
// // Calculate the number of correct answers
// const correctAnswers = questions.reduce((count, question, index) => {
// return count + (selectedAnswers[index] === question.answer ? 1 : 0);
// }, 0);
// // Display the number of correct answers
// scoreText.innerHTML = `Number of Correct Answers: ${correctAnswers}`;
// // Display all questions and answers
// const resultContainer = document.querySelector(".result_container");
// resultContainer.innerHTML = "";
// questions.forEach((question, index) => {
// const questionDiv = document.createElement("div");
// questionDiv.classList.add("question");
// const questionText = document.createElement("p");
// questionText.textContent = `Question ${question.numb}: ${question.question}`;
// questionDiv.appendChild(questionText);
// const userAnswer = selectedAnswers[index] || "Not answered";
// const userAnswerText = document.createElement("p");
// userAnswerText.textContent = `Your Answer: ${userAnswer}`;
// questionDiv.appendChild(userAnswerText);
// const correctAnswerText = document.createElement("p");
// correctAnswerText.textContent = `Correct Answer: ${question.answer}`;
// questionDiv.appendChild(correctAnswerText);
// resultContainer.appendChild(questionDiv);
// });
// }
//////
function startTimer(time){
counter = setInterval(timer, 1000);
function timer(){
timeCount.textContent = time; //changing the value of timeCount with time value
time--; //decrement the time value
if(time < 9){ //if timer is less than 9
let addZero = timeCount.textContent;
timeCount.textContent = "0" + addZero; //add a 0 before time value
}
if(time < 0){ //if timer is less than 0
clearInterval(counter); //clear counter
timeText.textContent = "Time Off"; //change the time text to time off
const allOptions = option_list.children.length; //getting all option items
let correcAns = questions[que_count].answer; //getting correct answer from array
for(i=0; i < allOptions; i++){
if(option_list.children[i].textContent == correcAns){ //if there is an option which is matched to an array answer
option_list.children[i].setAttribute("class", "option correct"); //adding green color to matched option
option_list.children[i].insertAdjacentHTML("beforeend", tickIconTag); //adding tick icon to matched option
console.log("Time Off: Auto selected correct answer.");
}
}
for(i=0; i < allOptions; i++){
option_list.children[i].classList.add("disabled"); //once user select an option then disabled all options
}
next_btn.classList.add("show"); //show the next button if user selected any option
}
}
}
function startTimerLine(time){
counterLine = setInterval(timer, 100);
function timer(){
time += 1; //upgrading time value with 1
time_line.style.width = time + "px"; //increasing width of time_line with px by time value
if(time > 549){ //if time value is greater than 549
clearInterval(counterLine); //clear counterLine
}
}
}
function queCounter(index){
//creating a new span tag and passing the question number and total question
let totalQueCounTag = '<span><p>'+ index +'</p> of <p>'+ questions.length +'</p> Questions</span>';
bottom_ques_counter.innerHTML = totalQueCounTag; //adding new span tag inside bottom_ques_counter
}