-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquizapp.js
183 lines (145 loc) · 5.41 KB
/
quizapp.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
const questionNumber = document.querySelector(".question-number");
const questionText = document.querySelector(".question-text");
const optionContainer = document.querySelector(".option-container");
let maxQuestions = quiz.length -15;
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;
function setAvailableQuestions() {
const totalQuestion = maxQuestions; //quiz.length;
for(let i=0; i<totalQuestion; i++){
availableQuestions.push(quiz[i])
}
}
function getNewQuestion() {
questionNumber.innerHTML = "Question " + (questionCounter + 1) + " of " + maxQuestions; //quiz.length;
const questionIndex = availableQuestions[Math.floor(Math.random() * availableQuestions.length)]
currentQuestion = questionIndex;
// console.log(questionIndex)
questionText.innerHTML = currentQuestion.q;
const index1 = availableQuestions.indexOf(questionIndex);
availableQuestions.splice(index1,1);
// console.log(questionIndex)
// console.log(index1)
const optionLen = currentQuestion.options.length;
console.log(currentQuestion.options);
console.log("Correct answer is index " + currentQuestion.answer);
for(let i=0; i<optionLen; i++){
availableOptions.push(i)
}
optionContainer.innerHTML = '';
let animationDelay = 0.14;
for(let i=0; i<optionLen; i++){
const optionIndex = availableOptions[Math.floor(Math.random() * availableOptions.length)];
const index2 = availableOptions.indexOf(optionIndex);
availableOptions.splice(index2,1);
// console.log(optionIndex)
// console.log(availableOptions)
const option = document.createElement("div");
option.innerHTML = currentQuestion.options[optionIndex];
option.id = optionIndex;
option.style.animationDelay = animationDelay + 's';
animationDelay = animationDelay + 0.14;
option.className = "option";
optionContainer.appendChild(option)
option.setAttribute("onclick","getResult(this)");
}
questionCounter++;
}
function getResult(element) {
// console.log(optionElement.innerHTML)
const id = parseInt(element.id);
console.log(typeof id)
if(id === currentQuestion.answer){
element.classList.add("correct");
updateAnswerIndicator("correct");
correctAnswers++;
//console.log("answer is correct");
}
else{
element.classList.add("wrong");
updateAnswerIndicator("wrong");
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");
}
}
console.log("answer is wrong");
}
attempt++;
unclickableOptions();
}
function unclickableOptions(){
const optionLen = optionContainer.children.length;
for(let i=0; i<optionLen; i++){
optionContainer.children[i].classList.add("already-answered");
}
}
function answersIndicator(){
const totalQuestion = maxQuestions; //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);
//console.log(markType)
}
function next() {
if(questionCounter === maxQuestions ){ //quiz.length
//console.log("Quiz Over");
quizOver();
}
else{
getNewQuestion();
}
}
function quizOver(){
quizBox.classList.add("hide");
resultBox.classList.remove("hide");
quizResult();
}
function quizResult(){
resultBox.querySelector(".total-question").innerHTML = maxQuestions; //quiz.length;
resultBox.querySelector(".total-attempt").innerHTML = attempt;
resultBox.querySelector(".total-correct").innerHTML = correctAnswers;
resultBox.querySelector(".total-wrong").innerHTML = attempt - correctAnswers;
const percentage = (correctAnswers/maxQuestions)*100; //quiz.length
resultBox.querySelector(".percentage").innerHTML =percentage.toFixed(2) + "%";
resultBox.querySelector(".total-score").innerHTML =correctAnswers +" / " + maxQuestions; //quiz.length;
}
function resetQuiz(){
questionCounter = 0;
correctAnswers = 0;
attempt = 0;
}
function tryAgainQuiz(){
resultBox.classList.add("hide");
quizBox.classList.remove("hide");
resetQuiz();
startQuiz();
}
function goToHome(){
resultBox.classList.add("hide");
homeBox.classList.remove("hide");
resetQuiz();
}
function startQuiz(){
homeBox.classList.add("hide");
quizBox.classList.remove("hide");
setAvailableQuestions();
getNewQuestion();
answersIndicator();
}
window.onload = function () {
homeBox.querySelector(".total-question").innerHTML = maxQuestions;
}