-
Notifications
You must be signed in to change notification settings - Fork 0
/
scriptquiz.js
150 lines (135 loc) · 3.75 KB
/
scriptquiz.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
const quiz = [
{
question: "What is the full form of HTML?",
options: [
"HyperText Markup Language",
"Hyper Text Memory Language",
"Both",
"None"
],
answer: "HyperText Markup Language"
},
{
question: "What does CSS stand for?",
options: [
"Cascading Style Sheets",
"Creative Style System",
"Computer Style Sheets",
"None of the above"
],
answer: "Cascading Style Sheets"
},
{
question: "What does JS stand for?",
options: [
"JavaScript",
"Java Style",
"Jumbo Script",
"None of the above"
],
answer: "JavaScript"
},
{
question: "Which HTML element is used to define an internal style sheet?",
options: [
"<style>",
"<css>",
"<script>",
"<link>"
],
answer: "<style>"
},
{
question: "Which of the following is a JavaScript framework?",
options: [
"React",
"Laravel",
"Django",
"None of the above"
],
answer: "React"
}
];
const quesnumber = document.getElementById("ques-no");
const quesel = document.getElementById("question-text");
const optionel = document.querySelectorAll(".option");
const timerel = document.getElementById("timer");
const nextbtn = document.getElementById("next");
const resultel = document.getElementById("result");
const scorel = document.getElementById("score");
let cq = 0;
let sc = 0;
let tl = 10;
let timer;
let answerselected = false;
function load() {
clearInterval(timer);
timer = setInterval(updateTimer, 1000);
const currentQuiz = quiz[cq];
quesnumber.textContent = `Question ${cq + 1} of ${quiz.length}`;
quesel.textContent = currentQuiz.question;
optionel.forEach((option, index) => {
option.textContent = currentQuiz.options[index];
option.classList.remove("correct", "incorrect");
option.onclick = () => selectOption(option);
});
tl = 10;
timerel.textContent = tl;
nextbtn.classList.add("hidden");
}
function updateTimer() {
if (tl > 0) {
tl--;
timerel.textContent = tl;
} else {
clearInterval(timer);
handleTimeout();
}
}
function handleTimeout() {
optionel.forEach(opt => {
if (opt.textContent === quiz[cq].answer) {
opt.classList.add("correct");
}
});
nextbtn.classList.remove("hidden");
}
message=document.querySelector("#message");
function selectOption(option) {
if (!answerselected) {
answerselected = true;
const selectedAnswer = option.textContent;
const correctAnswer = quiz[cq].answer;
if (selectedAnswer === correctAnswer) {
option.classList.add("correct");
message.textContent="Correct!";
sc++;
} else {
option.classList.add("incorrect");
message.textContent="InCorrect!";
optionel.forEach(opt => {
if (opt.textContent === correctAnswer) {
opt.classList.add("correct");
}
});
}
clearInterval(timer);
nextbtn.classList.remove("hidden");
}
}
nextbtn.onclick = () => {
answerselected = false;
nextbtn.classList.add("hidden");
if (cq < quiz.length - 1) {
cq++;
load();
} else {
showResult();
}
};
function showResult() {
clearInterval(timer);
resultel.classList.remove("hidden");
scorel.textContent = `Your score is ${sc} out of ${quiz.length}`;
}
load();