-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
160 lines (144 loc) · 5.05 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
let questions = [
{
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: "Wie bindet man eine Website in eine Website ein?",
answer_1: "<iframe>, <frame>, and <frameset>",
answer_2: "<iframe>",
answer_3: "<frame>",
answer_4: "<frameset>",
right_answer: 2,
},
{
question: "Wie stellt man Text am BESTEN fett dar?",
answer_1: "<strong>",
answer_2: "CSS nutzen",
answer_3: "<bold>",
answer_4: "<b>",
right_answer: 1,
},
{
question: "Welches Attribut kann man NICHT für Textarea verwenden?",
answer_1: "readonly",
answer_2: "max",
answer_3: "from",
answer_4: "spellcheck",
right_answer: 1,
},
{
question:
"Wie wählst du alle Elemente vom Typ <a> mit dem attribut title aus?",
answer_1: "a[title]{...}",
answer_2: "a > title {...}",
answer_3: "a.title {...}",
answer_4: "a=title {...}",
right_answer: 1,
},
{
question: "Wie definiert man in JavaScript eine Variable?",
answer_1: "let 100 = rate;",
answer_2: "100 = let rate;",
answer_3: "rate = 100;",
answer_4: "let rate = 100;",
right_answer: 4,
},
];
let rightQuestions = 0;
let currentQuestion = 0;
let AUDIO_SUCCESS = new Audio("audio/success.mp3");
let AUDIO_FAIL = new Audio("audio/fail.mp3");
function init() {
document.getElementById("all-questions").innerHTML = questions.length;
showQuestion();
}
function showQuestion() {
if (gameIsOver()) {
showEndScreen();
} else {
updateProgressBar();
updateToNextQuestion();
}
}
function gameIsOver() {
return currentQuestion >= questions.length;
}
function showEndScreen() {
document.getElementById("endScreen").style = "";
document.getElementById("questionBody").style = "display: none";
document.getElementById("amount-of-questions").innerHTML = questions.length;
document.getElementById("amount-of-right-questions").innerHTML =
rightQuestions;
document.getElementById("header-image").src = "img/trophy.png";
}
function updateProgressBar() {
let percent = (currentQuestion + 1) / questions.length;
percent = Math.round(percent * 100);
document.getElementById("progress-bar").innerHTML = `${percent} %`;
document.getElementById("progress-bar").style = `width: ${percent}%;`;
}
function updateToNextQuestion() {
let question = questions[currentQuestion];
document.getElementById("question-number").innerHTML = currentQuestion + 1;
document.getElementById("questiontext").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) {
let question = questions[currentQuestion]; // Frage aus dem Array holen
let selectedQuestionNumber = selection.slice(-1);
let idOfRightAnswer = `answer_${question["right_answer"]}`;
if (rightAnswerSelected(selectedQuestionNumber, question)) {
// Richtige Frage beantwortet
document.getElementById(selection).parentNode.classList.add("bg-success");
rightQuestions++;
} else {
document.getElementById(selection).parentNode.classList.add("bg-danger");
document
.getElementById(idOfRightAnswer)
.parentNode.classList.add("bg-success");
}
document.getElementById("next-button").disabled = false;
}
function rightAnswerSelected(selectedQuestionNumber, question) {
return selectedQuestionNumber == question["right_answer"];
}
function nextQuestion() {
currentQuestion++; // z.B. von 0 auf 1
document.getElementById("next-button").disabled = true;
resetAnswerButtons();
showQuestion();
}
function resetAnswerButtons() {
document.getElementById("answer_1").parentNode.classList.remove("bg-danger");
document.getElementById("answer_1").parentNode.classList.remove("bg-success");
document.getElementById("answer_2").parentNode.classList.remove("bg-danger");
document.getElementById("answer_2").parentNode.classList.remove("bg-success");
document.getElementById("answer_3").parentNode.classList.remove("bg-danger");
document.getElementById("answer_3").parentNode.classList.remove("bg-success");
document.getElementById("answer_4").parentNode.classList.remove("bg-danger");
document.getElementById("answer_4").parentNode.classList.remove("bg-success");
}
function restartGame() {
document.getElementById("header-image").src = "img/pencil.jpg";
document.getElementById("questionBody").style = ""; // questionBody wieder anzeigen
document.getElementById("endScreen").style = "display: none"; // Endscreen ausblenden
rightQuestions = 0;
currentQuestion = 0;
init();
}