-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
219 lines (171 loc) · 6.24 KB
/
script.js
File metadata and controls
219 lines (171 loc) · 6.24 KB
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
const body = document.querySelector('body');
const toggle = document.getElementById('toggle');
toggle.onclick = function(){
toggle.classList.toggle('active');
body.classList.toggle('active');
}
const questionNumber = document.querySelector(".question-number");
const questionText = document.querySelector(".question-text");
const optionContainer = document.querySelector(".option-container");
const answersIndicatorContainer = document.querySelector(".answers-indicator");
const homeBox = document.querySelector(".home-box");
const quizBox = document.querySelector(".quiz-box");
const resultBox =document.querySelector(".result-box");
let questionCounter = 0;
let currentQuestion;
let availableQuestions = [];
let availableOptions = [];
let correctAnswers = 0;
let attempt = 0;
// push the questions into availableQuestions Array
function setAvailableQuestions(){
const totalQuestion = quiz.length;
for(let i= 0; i<totalQuestion; i++){
availableQuestions.push(quiz[i])
}
}
// set question number and question and options
function getNewQuestion() {
// set question number
questionNumber.innerHTML = "Question " + (questionCounter +1) + " of " + quiz.length;
//set question text
// get random question
const questionIndex = availableQuestions[Math.floor(Math.random() * availableQuestions.length)]
currentQuestion = questionIndex;
questionText.innerHTML = currentQuestion.q;
// get the position of 'questionIndex' from the availableQuestion Array;
const index1 = availableQuestions.indexOf(questionIndex);
// remove the 'questionIndex' from the availableQuestion Array, to avoid repetition.
availableQuestions.splice(index1, 1);
// set options
// get the length of options
const optionLen = currentQuestion.options.length
for (let i = 0; i<optionLen; i++){
availableOptions.push(i)
}
optionContainer.innerHTML = '';
let animationDelay = 0.2;
// create options in HTML
for (let i = 0; i<optionLen; i++){
//random option
const optionIndex = availableOptions[Math.floor(Math.random() * availableOptions.length)];
//get the position of 'optionIndex' from the the 'availablieOption'
const index2 = availableOptions.indexOf(optionIndex);
//remove the 'optionIndex' from the availableOptions, to avoid repetition.
availableOptions.splice(index2, 1);
const option = document.createElement("div");
option.innerHTML = currentQuestion.options[optionIndex];
option.id = optionIndex;
option.style.animationDelay = animationDelay + 's';
animationDelay =animationDelay + 0.2;
option.className = "option";
optionContainer.appendChild(option)
option.setAttribute("onclick", "getResult(this)");
}
questionCounter++
}
//get the result of the current attempt question
function getResult(element) {
const id = parseInt(element.id);
// get the answer by comparing the id of clicked optio
if(id === currentQuestion.answer){
//set the green color to the correct option
element.classList.add("correct");
// add the indicator to correct mark
updateAnswerIndicator("correct");
correctAnswers++;
}
else{
// st the red color to the incorrect option
element.classList.add("wrong");
// add the indicatior to wrong mark
updateAnswerIndicator("wrong");
// if the answer is incorrect it shows the correct option
const optionLen = optionContainer.children.length;
for(let i = 0; i < optionLen; i++){
if(parseInt(optionContainer.children[i].id) === currentQuestion.answer){
optionContainer.children[i].classList.add("correct");
}
}
}
attempt++;
unclickableOptions();
}
// make all the options unclickable once the user select an option
function unclickableOptions(){
const optionLen = optionContainer.children.length;
for(let i = 0 ; i < optionLen; i++){
optionContainer.children[i].classList.add("already-answered")
}
}
function answersIndicator() {
answersIndicatorContainer.innerHTML = '';
const totalQuestion = quiz.length;
for(let i = 0; i < totalQuestion; i++){
const indicator = document.createElement("div");
answersIndicatorContainer.appendChild(indicator);
}
}
function updateAnswerIndicator(markType){
answersIndicatorContainer.children[questionCounter-1].classList.add(markType)
}
function next(){
if(questionCounter === quiz.length){
quizOver();
}else{
getNewQuestion();
}
}
function quizOver() {
//hide quiz quizBox
quizBox.classList.add("hide");
// show result Box
resultBox.classList.remove("hide");
quizResult();
}
function quizResult() {
resultBox.querySelector(".total-question").innerHTML = quiz.length;
resultBox.querySelector(".total-attempt").innerHTML = attempt;
resultBox.querySelector(".total-correct").innerHTML = correctAnswers;
resultBox.querySelector(".total-wrong").innerHTML = attempt - correctAnswers;
const percentage = (correctAnswers/quiz.length) * 100;
resultBox.querySelector(".percentage").innerHTML = percentage.toFixed(2) + "%";
resultBox.querySelector(".total-score").innerHTML = correctAnswers + " / " + quiz.length;
}
function resetQuiz(){
questionCounter = 0;
correctAnswers = 0;
attempt = 0;
}
function tryAgainQuiz(){
//hide the resultBoxx
resultBox.classList.add("hide");
// show the quizBox
quizBox.classList.remove("hide");
resetQuiz();
startQuiz();
}
function home(){
//hide result box
resultBox.classList.add("hide");
//show home box
homeBox.classList.remove("hide");
resetQuiz();
}
// ### STARTING POINT ###
function startQuiz() {
// hide home box
homeBox.classList.add("hide");
// show quiz Box
quizBox.classList.remove("hide");
resetQuiz();
// first we will see all questions in availableQuestions Array
setAvailableQuestions();
//second we will call getNewQuestio(); function
getNewQuestion();
//to create indicator of answers
answersIndicator();
}
// window.onload = function () {
// homeBox.querySelector(".total-question").innerHTML = quiz.length;
// }