-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
194 lines (169 loc) · 6.22 KB
/
script.js
File metadata and controls
194 lines (169 loc) · 6.22 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
var theQuiz = document.getElementById('theQuiz');
var pass = document.getElementById('pass');
var submitBtn = document.getElementById('submit');
var err = document.getElementById('err');
var errH = document.getElementById('errH');
var i;
// Adding event listener for the start quiz button
document.getElementById('start-quiz-btn').addEventListener('click', function()
{
document.getElementById('welcome-screen').style.display = 'none';
// Show the quiz container and initialize the quiz
document.getElementById('quiz-container').style.display = 'block';
startQuiz();
});
function startQuiz() {
theQuiz.style.display = 'block'; // show the quiz page
randomQ(); // trigger first 'random' question
}
var queDone = 0; // question asked
var userAns = []; // user's answers
var queDoneArr = []; // storing which question is asked
// showing levels (dots on top)
steps(totQ.length);
function steps() {
var mainStepDiv = document.getElementById('steps');
for (var i = 0; i <10; i++) {
var span = document.createElement('span');
span.className = 'step';
mainStepDiv.appendChild(span);
}
}
var p = document.getElementById('que'); // the question statement
//Answer options
var O1 = document.getElementById('opt1');
var O2 = document.getElementById('opt2');
var O3 = document.getElementById('opt3');
var O4 = document.getElementById('opt4');
// generates and places random questions
function randomQ() {
var x;
do {
x = Math.floor(Math.random() * totQ.length);
} while (totQ[x].asked === 1);
totQ[x].asked = 1;
queDoneArr.unshift(x);
queDone = ++queDone;
p.innerHTML = totQ[x].question;
// write options
O1.nextElementSibling.innerHTML = totQ[x].opt1;
O2.nextElementSibling.innerHTML = totQ[x].opt2;
O3.nextElementSibling.innerHTML = totQ[x].opt3;
O4.nextElementSibling.innerHTML = totQ[x].opt4;
if (!thisAsked) {
// If random number is already asked and this didn't become true then fire random question again
if (queDone != totQ.length)
randomQ(); // If not met the total length re-through random question
}
}
function next() {
if (!validateForm()) return false; // Validate if an option is selected
topping(queDone); // Update step indicator
if (queDone == 10) {
// If all questions are answered
theQuiz.style.display = 'none';
document.getElementById('theResult').style.display = 'block';
calcResult(); // Calculate and display result
return false;
} else {
randomQ(); // Otherwise, show next random question
}
}
var chkBox = document.getElementsByClassName('custom-control-input'); // targetting all checkboxes
// deals with validation of radio options and adds to the user's answer Array
function validateForm() {
var valid = false;
for (var i = 0; i < chkBox.length; i++) {
if (chkBox[i].checked) {
valid = true;
userAns.unshift(chkBox[i].value); // if 'checked' store user's answer
chkBox[i].checked = false;
nextBtn.setAttribute('disabled', 'disabled'); // otherwise, disbale button for next question
break;
}
}
if (!valid) {
// if no option selected
alert('Please Select Any Option...');
nextBtn.setAttribute('disabled', 'disabled');
}
if (valid)
document.getElementsByClassName('step')[queDone - 1].className += ' finish';
return valid; // return the valid status
}
// Enable NEXT button if option is checked
var nextBtn = document.getElementById('next-button');
function enableBtn(i) {
if (i.checked) nextBtn.removeAttribute('disabled');
else nextBtn.setAttribute('disabled', 'disabled');
}
// dynamic next button's text
function topping(n) {
if (n == totQ.length) {
document.getElementById('next-button').innerHTML = 'Submit';
calcResult();
} else {
document.getElementById('next-button').innerHTML = 'Next Question';
}
fixStepIndicator(n);
}
// Showing the active level from the total
function fixStepIndicator(n) {
var i,
x = document.getElementsByClassName('step');
for (i = 0; i < x.length; i++) {
x[i].className = x[i].className.replace(' active', ''); // removes the "active" class of all levels
}
x[n - 1].className += ' active'; // adds the "active" class on the current level
}
// calculates result
function calcResult() {
var ca = 0;
for (var i = 0; i < totQ.length; i++) {
var a = queDoneArr[i];
if (userAns[i] == totQ[a].answer) { // if user's answer matches with array's question's answer
ca = ca + 1; // increase correct answers' counter
}
}
// calculates percentage
var percentage = Math.round((ca / 9) * 100);// (dividing by 9 because everytime 9/10 questions are displayed)
showResult(percentage);
}
// Result part
var resultCircle = document.getElementById('resultCircle');
var resultFb = document.getElementById('resultFb');
var correctAns = document.getElementById('correctAns');
var quizCompleted = false;
var RColor;
function showResult(percentage) {
if (percentage == 100) {
RColor = 'pink';
resultFb.innerHTML = 'Wohoo...Great! You have passed.';
} else if (percentage >= 80) {
RColor = 'green';
resultFb.innerHTML = 'Congrats! You have passed.';
} else if (percentage >= 65) {
RColor = 'blue';
resultFb.innerHTML = 'Good Effort, You have passed.';
} else if (percentage >= 50) {
RColor = 'orange';
resultFb.innerHTML = 'You have passed!';
} else {
RColor = 'red';
resultFb.innerHTML = 'Oh No! Try Again';
}
localStorage.setItem('percentage', percentage);
quizCompleted = true;
var path = '<svg viewbox="0 0 36 36" class="circular-chart ' + RColor +'"> \
<path class="circle-bg" d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831" /> \
<path class="circle" stroke-dasharray="' + percentage + ', 100" d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831" /> \
<text x="19" y="21" id="percentage">' + percentage + '%</text> </svg>';
resultCircle.innerHTML = path;
}
// Retaking Quiz
function retakeQuiz()
{
localStorage.removeItem('percentage');
localStorage.removeItem('ca');
location.reload(true);
}