-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
227 lines (172 loc) · 5.93 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
const questionText=document.querySelector(".question-text");
const optionBox=document.querySelector(".option-box");
const currentQuestionNum=document.querySelector(".correct-question-num");
let questionIndex=0;
const correctAnswer=document.querySelector(".correct-answer");
const seeResultBtn=document.querySelector(".see-result-btn");
const remainingTime =document.querySelector(".remaining-time");
const nextQuestionBtn=document.querySelector(".next-question-btn");
const timeUpText=document.querySelector(".time-up-text");
const quizBox=document.querySelector(".quiz-box");
let number=0;
let score=0;
let interval;
myApp=[
{
question:"What is the method in JavaScript used to remove the whitespace at the beginning and end of any string ?",
options:["strip()","trim()","join()","split()"],
answer:1
},
{
question:"Which input type defines a slider control?",
options:["range","search","slider","controls"],
answer:0
},
{
question:"In HTML, which attribute is used to specify that an input field must be filled out?",
options:["validate","formvalidate","required","placeholder"],
answer:2
},
{
question:"What does CSS stand for?",
options:["Cascading Style Sheets ","Colorful Style Sheets","Creative Style Sheets","Computer Style Sheets"],
answer:0
},
{
question:"Which CSS property controls the text size?",
options:["text-size","font-style","text-style","font-size"],
answer:3
},
{
question:"How do you make each word in a text start with a capital letter?",
options:["text-style:capitalize","transform:capitalize","text-transform:capitalize","You can't do that with CSS"],
answer:2
},
{
question:"Which HTML attribute is used to define inline styles?",
options:["font","styles","style","class"],
answer:2
},
{
question:"In how many ways a JavaScript code can be involved in an HTML file?",
options:["1","2","3","4"],
answer:2
}
];
function load() {
number++;
questionText.innerHTML=myApp[questionIndex].question;
createOptions();
scoreBoard();
currentQuestionNum.innerHTML=number+ " / " + myApp.length;
}
function createOptions() {
optionBox.innerHTML="";
let animationDelay=0.2;
for(let i=0;i<myApp[questionIndex].options.length;i++)
{
const option=document.createElement("div");
option.innerHTML=myApp[questionIndex].options[i];
option.classList.add("option");
option.id=i;
option.style.animationDelay=animationDelay+"s";
animationDelay=animationDelay+0.2;
option.setAttribute("onClick","check(this)");
optionBox.appendChild(option);
}
}
function check(ele) {
const id=ele.id;
if(id==myApp[questionIndex].answer) {
ele.classList.add("correct");
score++;
scoreBoard();
}
else {
ele.classList.add("wrong");
for(let i=0;i<optionBox.children.length;i++){
if(optionBox.children[i].id==myApp[questionIndex].answer){
optionBox.children[i].classList.add("show-correct");
}
}
}
disableOptions();
showNextQuestionBtn();
stopTimer();
if(number==myApp.length)
{
quizOver();
}
}
function startTimer(){
let timeLimit=15;
remainingTime.innerHTML=timeLimit;
remainingTime.classList.remove("less-time");
interval=setInterval( () => {
timeLimit--;
if(timeLimit<10){
timeLimit="0"+timeLimit;
}
if(timeLimit<6){
remainingTime.classList.add("less-time");
}
remainingTime.innerHTML=timeLimit;
if(timeLimit==0){
clearInterval(interval);
timeIsUp();
}
},1000)
}
function stopTimer(){
clearInterval(interval);
}
function timeIsUp() {
showTimeUpText();
for(let i=0;i<optionBox.children.length;i++){
if(optionBox.children[i].id==myApp[questionIndex].answer){
optionBox.children[i].classList.add("show-correct");
}
}
disableOptions();
showNextQuestionBtn();
}
function disableOptions() {
for (let i = 0; i < optionBox.children.length; i++) {
optionBox.children[i].classList.add("already-answered");
}
}
function showNextQuestionBtn()
{
nextQuestionBtn.classList.add("show");
}
function hideNextQuestionBtn()
{
nextQuestionBtn.classList.remove("show");
}
function showTimeUpText() {
timeUpText.classList.add("show");
}
function hideTimeUpText() {
timeUpText.classList.remove("show");
}
function scoreBoard() {
correctAnswer.innerHTML=score;
}
nextQuestionBtn.addEventListener("click",nextQuestion);
function nextQuestion() {
questionIndex++;
load();
hideNextQuestionBtn();
hideTimeUpText();
startTimer();s
}
function quizOver () {
nextQuestionBtn.classList.remove("show");
seeResultBtn.classList.add("show");
}
seeResultBtn.addEventListener("click",() => {
})
window.onload=() => {
startTimer();
load();
}