-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz.js
197 lines (189 loc) · 5.76 KB
/
quiz.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
function $(selector, all = false) {
if (all) return document.querySelectorAll(selector);
return document.querySelector(selector);
}
let quizData = [{
question: "What is Stand for ARMY?",
a: "Alien Research Multiverse Yok",
b: "Ayush Raj Mohit Yammi",
c: "All Race Motors Yet",
d: "Ankush Raj Mahe Yam",
correct: "d",
}, {
question: "What is ARMY",
a: "Community of Leaders",
b: "Community of Content Creator",
c: "A Unity of BTS",
d: "All of the above",
correct: "b",
}, {
question: "Who is the founder of ARMY",
a: "Ankush Raj Mahe Yam",
b: "Ayush Raj Mahe Yam",
c: "Ansh Raj Mahe Yam",
d: "None of These",
correct: "a",
}, {
question: "ARMY company founded in",
a: "2000",
b: "2020",
c: "2024",
d: "none of the above",
correct: "b",
}, ];
let timeEle, timeBarEle, qNumberEle, formEle, nextEle, prevEle, questionEle, boxEle
let main = $("main")
let time, timer, currentQuiz, currentIndex = 1,
score = 0,
totalTime = 30
let startEle = $(".start")
startEle.onclick = start
function start() {
time = undefined
timer = undefined
currentQuiz = undefined
currentIndex = 1
score = 0
timeEle = undefined
quizData = [{
question: "What is Stand for ARMY?",
a: "Alien Research Multiverse Yok",
b: "Ayush Raj Mohit Yammi",
c: "All Race Motors Yet",
d: "Ankush Raj Mahe Yam",
correct: "d",
}, {
question: "What is ARMY",
a: "Community of Leaders",
b: "Community of Content Creator",
c: "A Unity of BTS",
d: "All of the above",
correct: "b",
}, {
question: "Who is the founder of ARMY",
a: "Ankush Raj Mahe Yam",
b: "Ayush Raj Mahe Yam",
c: "Ansh Raj Mahe Yam",
d: "None of These",
correct: "a",
}, {
question: "ARMY company founded in",
a: "2000",
b: "2020",
c: "2024",
d: "none of the above",
correct: "b",
}, ];
initialize(1)
}
function restartTimer() {
time = totalTime
timer = setInterval(() => {
if (time == 0) {
clearInterval(timer)
next()
return
}
time -= 1
timeEle.innerText = time + "s"
timeBarEle.style.width = (time / totalTime) * 100 + "%"
}, 1000)
}
function renueEle() {
timeEle = $(".time")
timeBarEle = $(".time-left")
qNumberEle = $(".q-number")
formEle = $("form")
nextEle = $(".next")
prevEle = $(".prev")
questionEle = $(".question")
boxEle = $(".box", true)
createListeners()
}
function initialize(index = 1) {
currentQuiz = quizData[index - 1]
console.log(currentQuiz.checked)
main.innerHTML = `
<div class="top-bar" >
<div>Question
<span class="q-number">${index}/${quizData.length}</span>
</div>
<div class="time" >30s</div>
</div>
<div class="time-bar" >
<div class="time-left" ></div>
</div>
<div class="question" >${currentQuiz.question}</div>
<form>
<div class="box" >
<input name="answer" type="radio" value="a" ${currentQuiz.checked=="a" ? "checked=checked" :''} />
<label> ${currentQuiz.a} </label>
</div>
<div class="box" >
<input c name="answer" type="radio" value="b" ${currentQuiz.checked=="b" ? "checked=checked" :''} />
<label>${currentQuiz.b}</label>
</div>
<div class="box" >
<input value="c" name="answer" type="radio" ${currentQuiz.checked=="c" ? "checked=checked" :''} />
<label> ${currentQuiz.c} </label>
</div>
<div class="box" >
<input value="d" name="answer" type="radio" ${currentQuiz.checked=="d" ? "checked=checked" :''} />
<label> ${currentQuiz.d} </label>
</div>
</form>
<div class="bottom-bar" >
<button class="prev ${index<=1 && 'hidden'}" >Prev
</button>
<button class="next" ${currentQuiz?.checked==undefined ? "disabled=disabled" :''} >${index>=quizData.length ? "See Result" :"Next"}</button>
</div>`
renueEle()
clearInterval(timer)
restartTimer()
}
function createListeners() {
/*event listeners */
boxEle.forEach(box => {
box.onclick = (e) => {
boxEle.forEach(box => box.classList.remove("active"))
box.querySelector("[name]").click()
box.classList.add("active")
}
})
formEle.onchange = (e) => {
const answer = e.target.value
nextEle.removeAttribute("disabled")
currentQuiz.checked = answer
if (currentQuiz.isCorrect == true) {
score -= 1
}
if (answer == currentQuiz.correct) {
score += 1
currentQuiz.isCorrect = true
}
}
nextEle.onclick = next
prevEle.onclick = prev
}
function next() {
currentIndex += 1
if (currentIndex > quizData.length) {
return seeResult()
}
initialize(currentIndex)
}
function prev() {
currentIndex -= 1
initialize(currentIndex)
}
function seeResult() {
main.innerHTML = `
<div class="question" > You scored
<b>${score}</b> out of
<b>${quizData.length}</b>
</div>
<button class="start" >Restart</button>
`
startEle = $(".start")
startEle.onclick = start
}