-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
278 lines (255 loc) · 7.16 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
let HtmlQuestions = [
{
question: "Wer hat HTML erfunden?",
answer_1: "Robbie Williams",
answer_2: "Lady Gaga",
answer_3: "Tim Berners-Lee",
answer_4: "Justin Bieber",
right_answer: 3,
},
{
question: "Was bedeutet das HTML Tag <a>?",
answer_1: "Text fett",
answer_2: "Container",
answer_3: "Ein Link",
answer_4: "Kursiv",
right_answer: 3,
},
{
question: "Mit welchem Tag bindet man Bilder ein?",
answer_1: "<image>",
answer_2: "<img>",
answer_3: "<pic>",
answer_4: "<picture>",
right_answer: 2,
},
{
question: "Wofür steht der Tag <b>?",
answer_1: "unterstrichen",
answer_2: "kursiv",
answer_3: "durchgestrichen",
answer_4: "fett",
right_answer: 4,
},
{
question: "Mit welchem Tag fügt man einen Container ein?",
answer_1: "<div>",
answer_2: "<link>",
answer_3: "<a>",
answer_4: "<img>",
right_answer: 1,
},
];
let CssQuestions = [
{
question: "Mit welchem Tag kann man im Header CSS Dateien einbinden?",
answer_1: "stylesheet",
answer_2: "src",
answer_3: "link",
answer_4: "meta",
right_answer: 3,
},
{
question: "Mit welchem CSS-Attribut kann man text zentrieren?",
answer_1: "center",
answer_2: "margin",
answer_3: "text-align",
answer_4: "padding",
right_answer: 3,
},
{
question: "Welches CSS-Attribut steht für den Aussenabstand?",
answer_1: "padding",
answer_2: "margin",
answer_3: "height",
answer_4: "width",
right_answer: 2,
},
{
question: "Mit welcher Psuedo-Klasse kann man den Effekt beim Darüberfahren anpassen?",
answer_1: ":active",
answer_2: ":disabled",
answer_3: ":isvalid",
answer_4: ":hover",
right_answer: 4,
},
{
question: "Welches Attribut verändert die Schriftfarbe?",
answer_1: "color",
answer_2: "style",
answer_3: "background-color",
answer_4: "border-color",
right_answer: 1,
},
];
let JsQuestions = [
{
question: "Wie greift man auf Elemente über die ID zu?",
answer_1: "getElementsByClassName()",
answer_2: "getElementsByTagName()",
answer_3: "getElementById()",
answer_4: "getElementsByName()",
right_answer: 3,
},
{
question: "Wie fügt man einem Array Werte hinzu?",
answer_1: "add()",
answer_2: "append()",
answer_3: "push()",
answer_4: "innerHTML()",
right_answer: 3,
},
{
question: "Wie fügt man einem Element eine CSS-Klasse hinzu?",
answer_1: "Element.addClass",
answer_2: "Element.classList.add",
answer_3: "Element.style",
answer_4: "Element.value",
right_answer: 2,
},
{
question: "Wie definiert man eine Funktion?",
answer_1: "def name() {}",
answer_2: "func name() {}",
answer_3: "class name() {}",
answer_4: "function name() {}",
right_answer: 4,
},
{
question: "Wie beende man vorzeitig eine Funktion?",
answer_1: "return",
answer_2: "break",
answer_3: "continue",
answer_4: "exit",
right_answer: 1,
},
];
const successSound = new Audio("audio/success.mp3");
const errorSound = new Audio("audio/error.mp3");
let questions = [];
let currentQuestion = 0;
let rightAnswers = 0;
function loadQuestions(section) {
if (section == "html") return HtmlQuestions;
if (section == "css") return CssQuestions;
if (section == "js") return JsQuestions;
alert("Section not implemented");
}
function start(element, section) {
setActiveNavlink(element);
showStartingContent(false);
showEndScreen(false);
resetProgressBar();
resetSavedAnswers();
resetAnswerButtons();
showQuestion(true);
questions = loadQuestions(section);
setQuestion();
}
function showStartingContent(boolean) {
if (boolean) {
document.getElementById("starting-content").style = "display:unset";
} else {
document.getElementById("starting-content").style = "display: none";
}
}
function resetSavedAnswers() {
currentQuestion = 0;
rightAnswers = 0;
}
function showQuestion(boolean) {
if (boolean) {
document.getElementById("question-body").style = "display:unset";
} else {
document.getElementById("question-body").style = "display:none";
}
}
function showEndScreen(boolean) {
if (boolean) {
document.getElementById("end-screen").style = "display:flex";
} else {
document.getElementById("end-screen").style = "display:none";
}
}
function setQuestion() {
if (currentQuestion >= questions.length) {
showQuestion(false);
showEndScreen(true);
document.getElementById("score").innerHTML = `${rightAnswers} / ${questions.length}`;
} else {
enableNextButton(false);
let question = questions[currentQuestion];
document.getElementById("question").innerHTML = question.question;
document.getElementById("answer_1").innerHTML = question.answer_1;
document.getElementById("answer_2").innerHTML = question.answer_2;
document.getElementById("answer_3").innerHTML = question.answer_3;
document.getElementById("answer_4").innerHTML = question.answer_4;
}
}
function answer(selection) {
updateProgressBar();
let question = questions[currentQuestion];
let selectedQuestionNumber = selection.slice(-1);
let idOfRightAnswer = `answer_${question.right_answer}`;
if (Number(selectedQuestionNumber) === question.right_answer) {
document.getElementById(selection).parentNode.classList.add("container-success");
rightAnswers++;
successSound.play();
} else {
document.getElementById(selection).parentNode.classList.add("container-danger");
document.getElementById(idOfRightAnswer).parentNode.classList.add("container-success");
errorSound.play();
}
enableNextButton(true);
enableAnswers(false);
}
function enableAnswers(boolean) {
for (let answer of document.getElementsByClassName("answer-container")) {
if (boolean) {
answer.style["pointer-events"] = "auto";
} else {
answer.style["pointer-events"] = "none";
}
}
}
function enableNextButton(boolean) {
document.getElementById("next-button").disabled = !boolean;
}
function nextQuestion() {
currentQuestion++;
resetAnswerButtons();
enableNextButton(false);
setQuestion();
enableAnswers(true);
}
function resetAnswerButtons() {
for (let answer of document.getElementsByClassName("answer-container")) {
answer.classList.remove("container-success");
answer.classList.remove("container-danger");
}
}
function shareScore() {
alert("nicht implementiert");
}
function replay() {
currentQuestion = 0;
rightAnswers = 0;
resetProgressBar();
showEndScreen(false);
showQuestion(true);
}
function updateProgressBar() {
const progressLength = ((currentQuestion + 1) / questions.length) * 100;
document.getElementById("current-progress").style = `width: ${progressLength}%`;
document.getElementById("current-progress").innerHTML = `${progressLength}%`;
}
function resetProgressBar() {
document.getElementById("current-progress").style = "display: none";
}
function setActiveNavlink(element) {
const navlinks = document.getElementsByClassName("navlink");
for (let i = 0; i < navlinks.length; i++) {
navlinks[i].classList.remove("active");
}
element.classList.add("active");
}