-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz.js
executable file
·171 lines (153 loc) · 6.12 KB
/
quiz.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
/*
██ ██ ██ ████ ██████ ██ ██ ██████ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ████ ██ ██ ██
██ ██ ██ ████████ ██████ ██ ████ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██████ ██ ██ ██████ ██
████ ██████ ████ ██████ ██████ ██ ██ ████ ████ ██████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██____
██ ██████ ████████ ██████ ██████ ██ ██ ██ ██ ██ ██ ██
████ ██ ██ ██ ██ ██ ██ ██ ████ ████ ██████ ██████
...and crappy ANSI art...
But it works! (at least, i hope it works...)
I wouldn't advise to read it, you'll think you know nothing of life because you don't know how this could possibly work...
-----DEPRESSION ALERT!!!!!-----
made by Nathan Huisman
*/
let newLine = String.fromCharCode(0x0A);
let XMLArray;
let quizDoneArray = [];
document.addEventListener("load", main());
function main() {
loadQuiz("quiz.xml");
}
async function loadQuiz(url) {
let response = await fetch(url);
let XMLText = await response.text();
let regex = /<q>((\s|.)*?)(?=<\/q>)/g;
XMLArray = XMLText.match(regex);
for (i = 0; i < XMLArray.length; i++) {
let XML = XMLArray[i].slice(XMLArray[i].indexOf(">") + 1);
XML = replaceTag(XML, "text", "h1");
let answerType = "multiple-choice-radio";
console.log(answerType);
switch (answerType) {
case "text": {
XML = replaceTag(XML, "answer", "input", "type=\"text\"");
}
case "text-long": {
XML = replaceTag(XML, "answer", "textarea");
}
case "multiple-choice-radio": {
XML = XML.replace(/<a(|( [^]*?))>[^]*?<\/a>/g, (match) => {
return "<p><span class=\"checkbox\"></span>" + match + "</p>";
});
XML = replaceTag(XML, "answer", "div");
XML = replaceTag(XML, "a", "input", "type=\"radio\" name=\"multiple-choice-" + i + "\"", true);
}
case "multiple-choice-check": {
XML = XML.replace(/<a(|( [^]*?))>[^]*?<\/a>/g, (match) => {
return "<p>" + match + " <br /> </p>";
});
XML = replaceTag(XML, "answer", "div");
XML = replaceTag(XML, "a", "input", "type=\"checkmark\" name=\"multiple-choice-" + i + "\"", true);
}
}
XML = replaceTag(XML, "des", "p", "id=\"answer\"");
XML += newLine + "<button class=\"continue\" onclick=\"goToPage(" + (i + 1) + ");\">Antwoord</button>";
XMLArray[i] = XML;
}
document.querySelector("#quiz").innerHTML = XMLArray[0];
let quizEls = document.querySelectorAll("#quiz > * > p");
if (quizEls) {
for (i = 0; i < quizEls.length; i++) {
quizEls[i].addEventListener("click", evt => {
let button = evt.target.querySelector("input");
if (!button) return;
button.click();
});
}
}
}
function replaceTag(text, tag, newTag, newAttr, keepAttr) {
newAttr = newAttr ? " " + newAttr : "";
return text.replace(new RegExp("<(\/?" + tag + "([^]*?))>", "g"), (match, noBr, attr) => {
if (!keepAttr) {
attr = "";
}
if (noBr.endsWith("/")) {
return "<" + newTag + newAttr + attr + " />";
}
else if (noBr.charAt(0) === "/") {
return "</" + newTag + ">";
}
else {
return "<" + newTag + newAttr + attr + ">";
}
});
}
function goToPage(page) {
let quizWindow = document.querySelector("#quiz");
let quizElms = quizWindow.querySelectorAll("*");
quizDoneArray.push(quizWindow.querySelectorAll("*"));
if (page >= XMLArray.length) {
finishQuiz(quizDoneArray);
console.log(quizDoneArray);
return;
}
for (let i = 0; i < quizElms.length; i++) {
if (quizElms[i].tagName.toLowerCase() === "button") {
quizElms[i].innerHTML = "Volgende vraag";
quizElms[i].removeAttribute("onclick");
quizElms[i].addEventListener("click", () => {
quizWindow.innerHTML = XMLArray[page];
let quizEls = document.querySelectorAll("#quiz > * > p");
if (quizEls) {
for (i = 0; i < quizEls.length; i++) {
quizEls[i].addEventListener("click", evt => {
let button = evt.target.querySelector("input");
if (!button) return;
button.click();
});
}
}
});
continue;
}
quizElms[i].style.display = "none";
}
quizWindow.querySelector("p#answer").style.display = "block";
}
async function getAllHTMLElements() {
let response = await fetch("https://api.github.com/repos/mdn/browser-compat-data/contents/html/elements");
let responseJSON = await response.json();
let nameArray = [];
let fetchArray = [];
let getJSONArray = [];
let JSONArray = [];
console.log(responseJSON);
for (i = 0; i < responseJSON.length; i++) {
const element = responseJSON[i];
nameArray.push(element.name.split(".")[0]);
fetchArray.push(fetch(element.url));
}
let fetchResponses = await Promise.all(fetchArray);
for (i = 0; i < fetchResponses.length; i++) {
getJSONArray.push(fetchResponses[i].json());
}
JSONArray = await Promise.all(fetchArray);
console.log(JSONArray);
}
function finishQuiz(quizArr) {
let points = 0;
for (let i = 0; i < quizArr.length; i++) {
for (let j = 0; j < quizArr[i].length; j++) {
if (quizArr[i][j].tagName.toLowerCase() !== "input") continue;
if (quizArr[i][j].attributes.correctAnswer && quizArr[i][j].checked) {
points++;
console.log(points);
}
}
}
let quizWindow = document.querySelector("#quiz");
quizWindow.innerHTML = "<h1>De quiz is afgelopen. je hebt " + points + " van de " + quizArr.length + " vragen goed!</h1>";
}