-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.js
215 lines (178 loc) · 6.55 KB
/
js.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
"use strict"
const quiz = document.getElementById("quiz");
let cards = document.getElementsByClassName("card");
let quizCards = document.getElementsByClassName("quiz-api");
let optionsElements;
let nextButtons;
let counters;
let pointsGained = 0;
let countdownType = "";
let correctAnswers = [true];
let focusableArrays = [];
if ('scrollRestoration' in window.history) {
window.history.scrollRestoration = 'manual';
}
createQuestions();
async function createQuestions() {
let response = await fetch("https://quizapi.io/api/v1/questions?apiKey=qt9GpHOhGOY91eTnWxb4CBrGUOrq807bGmipXFqh&tags=javascript&limit=10&multiple_correct_answers=false");
let quizApi = await response.json();
for (let [i, question] of Object.entries(quizApi)) {
getCorrectAnswer(question);
question.answers = randomizeAnswers(question.answers);
insertQuestion(question, i);
}
getTabIndexElements();
optionsElements = quiz.getElementsByClassName("options");
nextButtons = quiz.getElementsByClassName("next");
counters = quiz.getElementsByClassName("counter");
optionListeners();
nextPageListener();
document.getElementById("select").classList.remove("hidden");
}
function getCorrectAnswer(question) {
//index name of question.correct_answers with value "true" points us to the content of the correct answer.
let correctIndex = Object.entries(question.correct_answers).find(answer => answer[1] === "true")[0];
//But first we need to get rid of additional last word
correctIndex = correctIndex.split("_").slice(0, 2).join("_");
correctAnswers.push(question.answers[correctIndex]);
}
function randomizeAnswers(answers) {
answers = Object.values(answers).filter(Boolean);
let random, length = answers.length;
while (length) {
random = Math.floor(Math.random() * length--);
[answers[length], answers[random]] = [answers[random], answers[length]];
}
return answers;
}
function insertQuestion(quizContent, index) {
let quiestionHeader = escapeHtmlCharacters(quizContent.question);
let optionButtons = "";
for (let answer of quizContent.answers) {
answer = escapeHtmlCharacters(answer);
optionButtons += `<button class="option">${answer}</button>` + "\n";
}
quizCards[index].innerHTML = `<h2 class="question">${quiestionHeader}</h2>
<div class="options">
${optionButtons}
</div>
<div class="progress">
<button class="next hidden">Start</button>
<div class="counter"></div>
</div>`;
}
function escapeHtmlCharacters(htmlText) {
let map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return htmlText.replace(/[&<>"']/g, char => map[char]);
}
function getTabIndexElements () {
for(let quizElement of quiz.children) {
focusableArrays.push(quizElement.querySelectorAll("button, a"));
}
for(let [i, focusableArray] of Object.entries(focusableArrays)) {
if (i != 0) {
blockTabIndex(focusableArray);
}
}
}
function blockTabIndex(array) {
for(let focusableElement of array) {
focusableElement.tabIndex = "-1";
}
}
function unblockTabIndex(array) {
for(let focusableElement of array) {
focusableElement.tabIndex = "0";
}
}
function optionListeners() {
for (let [i, options] of Object.entries(optionsElements)) {
for (let [j, option] of Object.entries(options.children)) {
option.addEventListener("click", () => {
disableButtons(i);
option.classList.add("clicked");
if (i == 0) { //first questions is about difficulty.
setDifficulty(j);
} else {
checkAnswer(option.innerText, i);
if(i == 10) {
createFooter();
}
}
});
}
}
}
function nextPageListener() {
// let scrollTimeout;
for (let [i, nextButton] of Object.entries(nextButtons)) {
i = +i;
if (i === 11) {
return;
}
nextButton.addEventListener("click", (e) => {
blockTabIndex(focusableArrays[i]);
if(i != 10) {
cards[i].addEventListener("animationend", () => {
counters[i + 1].classList.add(countdownType);
counters[i + 1].innerHTML = "";
unblockTabIndex(focusableArrays[i + 1]);
})
}
cards[i + 1].classList.remove("hidden-card");
cards[i].classList.add("fade");
})
}
}
function setDifficulty (optionIndex) {
countdownType = optionIndex == 0 ? "count-down-easy" : "count-down-hard";
for(let [i, counter] of Object.entries(counters)) {
if(i != 0 && i != 11) { // only questions have working counters
counter.innerHTML = countdownType == "count-down-easy" ? 10 : 3;
counter.addEventListener("animationend", (event) => {
CheckForBeforeAnimation(event, i);
});;
}
}
}
function CheckForBeforeAnimation(event, counterIndex) {
if(event.pseudoElement == "::before") {
disableButtons(counterIndex);
}
}
function disableButtons(i) {
nextButtons[i].classList.remove("hidden");
for (let option of optionsElements[i].children) {
option.disabled = true;
}
}
function checkAnswer(answer, i) {
let isAnswerCorrect = answer == correctAnswers[i];
pointsGained += Number(isAnswerCorrect);
counters[i].innerHTML = isAnswerCorrect ? '<i class="fas fa-check"></i>' : '<i class="fas fa-times"></i>';
counters[i].classList.remove("count-down-hard", "count-down-easy");
}
function createFooter() {
let scoreComment;
if(pointsGained == 10) {
scoreComment = "Perfect!";
} else if (pointsGained > 7) {
scoreComment = "Great Job!";
} else if (pointsGained > 4) {
scoreComment = "Nice score!";
} else {
scoreComment = "Ouch, Maybe next time.";
}
cards[11].innerHTML += `<h2>You scored ${pointsGained}/10!</h2>
<p>${scoreComment} This Quiz was done for my dev.to <a target="_blank" href="https://dev.to/dgx32123/working-with-apis-39ad" target="_blank">blog post</a>, if you liked or disliked it I encourage you to leave your feedback in the comments.</p>
<button class="next" id="refresh">Press F5 to Try Again</button>`;
document.getElementById("refresh").addEventListener("click", () => {
location.reload();
})
}