-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
169 lines (128 loc) · 4.75 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
const startBtn = document.querySelector('.start-btn');
const popupInfo = document.querySelector('.popup-info');
const exitBtn = document.querySelector('.exit-btn');
const main = document.querySelector('.main');
const continueBtn = document.querySelector('.continue-btn');
const quizSection = document.querySelector('.quiz-section');
const quizBox = document.querySelector('.quiz-box');
const resultBox = document.querySelector('.result-box');
const tryAgainBtn = document.querySelector('.TryAgain-btn');
const goHomeBtn = document.querySelector('.goHome-btn');
startBtn.onclick = () => {
popupInfo.classList.add('active');
main.classList.add('active');
}
exitBtn.onclick = () => {
popupInfo.classList.remove('active');
main.classList.remove('active');
}
continueBtn.onclick = () => {
quizSection.classList.add('active');
popupInfo.classList.remove('active');
main.classList.remove('active');
quizBox.classList.add('active');
showQuestions(0);
questionCounter(1);
headerScore();
}
tryAgainBtn.onclick = () => {
quizBox.classList.add('active');
nextBtn.classList.remove('active');
resultBox.classList.remove('active');
questionCount = 0;
questionNumb = 1;
userScore = 0;
showQuestions(questionCount);
questionCounter(questionNumb);
headerScore();
}
goHomeBtn.onclick = () => {
quizSection.classList.remove('active');
nextBtn.classList.remove('active');
resultBox.classList.remove('active');
questionCount = 0;
questionNumb = 1;
userScore = 0;
showQuestions(questionCount);
questionCounter(questionNumb);
}
let questionCount = 0;
let questionNumb = 1;
let userScore = 0;
const nextBtn = document.querySelector('.next-btn');
nextBtn.onclick = () => {
if (questionCount < questions.length - 1) {
questionCount++;
showQuestions(questionCount);
questionNumb++;
questionCounter(questionNumb);
nextBtn.classList.remove('active');
}
else {
showResultBox();
}
}
const optionList = document.querySelector('.option-list');
function showQuestions(index) {
const questionText = document.querySelector('.question-text');
questionText.textContent = `${questions[index].numb}. ${questions[index].question}`;
let optionTag = `<div class="option"><span>${questions[index].options[0]}</span></div>
<div class="option"><span>${questions[index].options[1]}</span></div>
<div class="option"><span>${questions[index].options[2]}</span></div>
<div class="option"><span>${questions[index].options[3]}</span></div>`;
optionList.innerHTML = optionTag;
const option = document.querySelectorAll('.option');
for (let i = 0; i < option.length; i++) {
option[i].setAttribute('onclick', 'optionSelected(this)');
}
}
function optionSelected(answer) {
let userAnswer = answer.textContent;
let correctAnswer = questions[questionCount].answer;
let allOptions = optionList.children.length;
if (userAnswer == correctAnswer) {
answer.classList.add('correct');
userScore += 1;
headerScore();
}
else {
answer.classList.add('incorrect');
for (let i = 0; i < allOptions; i++) {
if (optionList.children[i].textContent == correctAnswer) {
optionList.children[i].setAttribute('class', 'option correct');
}
}
}
for (let i = 0; i < allOptions; i++) {
optionList.children[i].classList.add('disabled');
}
nextBtn.classList.add('active');
}
function questionCounter(index) {
const questionTotal = document.querySelector('.question-total');
questionTotal.textContent = `${index} of ${questions.length} Questions`;
}
function headerScore() {
const headerScoreText = document.querySelector('.header-score');
headerScoreText.textContent = `score: ${userScore} / ${questions.length}`;
}
function showResultBox() {
quizBox.classList.remove('active');
resultBox.classList.add('active');
const scoreText = document.querySelector('.score-text');
scoreText.textContent = `Your Score ${userScore} out of ${questions.length}`;
const circularProgress = document.querySelector('.circular-progress');
const progressValue = document.querySelector('.progress-value');
let progressStartValue = -1;
let progressEndValue = (userScore / questions.length) * 100;
let speed = 20;
let progress = setInterval(() => {
progressStartValue++;
//console.log(progressStartValue);
progressValue.textContent = `${progressStartValue}%`;
circularProgress.style.background = `conic-gradient(#c40094 ${progressStartValue * 3.6}deg, rgba(255, 255, 255, .1) 0deg)`;
if (progressStartValue == progressEndValue) {
clearInterval(progress);
}
}, speed);
}