-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
232 lines (201 loc) · 7.07 KB
/
app.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
const start_btn = document.querySelector('.start');
const rules_box = document.querySelector('.quiz-rules-block');
const exit_btn = rules_box.querySelector('.exit');
const continue_btn = rules_box.querySelector('.continue');
const quiz_box = document.querySelector('.quiz-box');
const time_sec = quiz_box.querySelector('.quiz-timer-sec');
const no_ques = quiz_box.querySelector('.quiz-ques-attend');
const time_line = quiz_box.querySelector('header .time-line');
const result_box = document.querySelector('.result-box');
const time_off = quiz_box.querySelector('.quiz-timer-text');
const close_btn = quiz_box.querySelector('.close-btn');
// onclick start btn
start_btn.addEventListener('click', function(){
rules_box.classList.add('show-info');
});
// onclick exit btn
exit_btn.addEventListener('click', function(){
rules_box.classList.remove('show-info');
});
// onclick continue btn
let quescount = 0;
let counter;
let counterLine;
let time_value = 15;
let width_value = 0;
let user_score = 0;
continue_btn.addEventListener('click', function(){
rules_box.classList.remove('show-info');
quiz_box.classList.add('show-ques-block');
next(quescount);
no_question_attended(quescount);
countSec(15);
countSecLine(0);
});
function next(index){
const questions = quiz_box.querySelector('.quiz-box-ques')
const options = quiz_box.querySelectorAll('.quiz-box-option');
let question_tag = `<h2>${ques[index].id}. ${ques[index].ques}</h2>`;
for(var x = 0; x<options.length; x++){
let options_tag = `<span class="option">${ques[index].opt[x]}</span>`;
const option_clicked = quiz_box.querySelector('.option')
options[x].innerHTML = options_tag;
options[x].setAttribute("onclick","optionSelectd(this,"+index+","+x+")");
for(var k =0; k<op.length; k++){
op[k].classList.remove('correct');
op[k].classList.remove('wrong');
op[k].classList.remove('disabled');
}
}
questions.innerHTML = question_tag;
}
const correct_count = 0;
// Answer
const op = quiz_box.querySelectorAll('.quiz-box-option');
const next_btn = quiz_box.querySelector('.next-btn-inner');
// Icons
const tick_icon = quiz_box.querySelector('.tick');
const cross_icon = quiz_box.querySelector('.cross');
function optionSelectd(ans, i, x){
clearInterval(counter);
clearInterval(counterLine);
let user_value = ans.textContent;
let correct_value = ques[quescount].answer;
if(correct_value == user_value){
user_score++;
console.log(user_score);
op[x].classList.add('correct');
op[x].insertAdjacentElement('beforeend', tick_icon);
} else {
op[x].classList.add('wrong');
op[x].insertAdjacentElement('beforeend', cross_icon);
op.forEach(function(item){
const demo = item.querySelector('.option');
if(demo.textContent == correct_value){
item.classList.add('correct');
console.log('correct ans is ', demo.textContent);
item.insertAdjacentElement('beforeend', tick_icon);
}
});
}
for(var j=0; j<op.length; j++){
op[j].classList.add('disabled');
}
next_btn.style.display = "block";
}
// onclick close btn
close_btn.addEventListener('click', function(){
quiz_box.classList.remove('show-ques-block');
quescount = 0;
time_value = 15;
width_value = 0;
user_score = 0;
quescount = 0;
});
// Question Next button
next_btn.addEventListener('click', function(){
quescount++;
if(quescount < ques.length){
next(quescount);
clearInterval(counter);
countSec(time_value);
clearInterval(counterLine);
countSecLine(width_value);
next_btn.style.display = "none";
} else {
clearInterval(counter);
clearInterval(counterLine);
result_block();
}
no_question_attended(quescount);
});
// No of Question
function no_question_attended(quescount){
let no_ques_tag = `<span><p>${quescount+1}</p>of<p>${ques.length}</p>Questions</span>`;
no_ques.innerHTML = no_ques_tag;
}
// Result Box
const restart_btn = result_box.querySelector('.restart');
const result_quit = result_box.querySelector('.quit');
const result_content = result_box.querySelector('.some-content');
function result_block(){
quiz_box.classList.remove('show-ques-block');
result_box.classList.add('show-result-block');
if(user_score > 3){
let result_content_tag = `<h1>You've Completed the Quiz!.</h1>
<h2>and Congrats!!, You got Only <p>${user_score}</p>out of<p>${ques.length}</p>.</h2>`;
result_content.innerHTML = result_content_tag;
}
else if(user_score > 2){
let result_content_tag = `<h1>You've Completed the Quiz!.</h1>
<h2>and Nice!, You got Only <p>${user_score}</p>out of<p>${ques.length}</p>.</h2>`;
result_content.innerHTML = result_content_tag;
}
else{
let result_content_tag = `<h1>You've Completed the Quiz!.</h1>
<h2>and Sorry, You got Only <p>${user_score}</p>out of<p>${ques.length}</p>.</h2>`;
result_content.innerHTML = result_content_tag;
}
user_score = 0;
}
restart_btn.addEventListener('click', function(){
result_box.classList.remove('show-result-block');
quiz_box.classList.add('show-ques-block');
quescount = 0;
time_value = 15;
width_value = 0;
user_score = 0;
quescount = 0;
next(quescount);
clearInterval(counter);
countSec(time_value);
clearInterval(counterLine);
countSecLine(width_value);
next_btn.style.display = "none";
no_question_attended(quescount);
});
result_quit.addEventListener('click', function(){
result_box.classList.remove('show-result-block');
quescount = 0;
});
// Sec Counter
function countSec(t){
counter = setInterval(timer, 1000);
function timer(){
time_sec.textContent = t;
t--;
if(t < 9){
let addZero = time_sec.textContent;
time_sec.textContent = '0'+addZero;
}
if(t < 0){
clearInterval(counter);
time_sec.textContent = '00';
time_off.textContent = "Time Off"
let correct_value = ques[quescount].answer;
op.forEach(function(item){
const demo = item.querySelector('.option');
if(demo.textContent == correct_value){
item.classList.add('correct');
console.log('correct ans is ', demo.textContent);
item.insertAdjacentElement('beforeend', tick_icon);
}
});
for(var j=0; j<op.length; j++){
op[j].classList.add('disabled');
}
next_btn.style.display = "block";
}
}
}
// Timeline
function countSecLine(t){
counterLine = setInterval(timer, 27);
function timer(){
t += 1;
time_line.style.width = t+"px";
if(t > 599){
clearInterval(counterLine);
}
}
}