-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
154 lines (130 loc) · 4.96 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
let questions = [
{
"question": "Wer hat HTML erfunden?",
"answer-1": "Bill Gates",
"answer-2": "Vinton Cerf",
"answer-3": "Tim Berners-Lee",
"answer-4": "Robert Kahn",
"right-answer": 3
},
{
"question": "Wie zentriert man ein Element auf der x-Achse?",
"answer-1": "justify-content: center",
"answer-2": "justify-content: middle",
"answer-3": "align-items: center",
"answer-4": "align-items: middle",
"right-answer": 1
},
{
"question": "Wie zentriert man ein Element auf der y-Achse?",
"answer-1": "justify-content: center",
"answer-2": "justify-content: middle",
"answer-3": "align-items: center",
"answer-4": "align-items: middle",
"right-answer": 3
},
{
"question": "Mit welcher Programiersprache bestimmt man das Design einer Webseite?",
"answer-1": "HTML",
"answer-2": "JavaScript",
"answer-3": "Python",
"answer-4": "CSS",
"right-answer": 4
},
{
"question": "Mit welcher JavaScript funktion kann man die Nachkommastellen einer Zahl festlegen??",
"answer-1": "parseFloat()",
"answer-2": "toFixed()",
"answer-3": "valueOf()",
"answer-4": "toLocaleString()",
"right-answer": 2
}
]
let currentQuestion = 0;
let rightQuestions = 0;
let audio_success = new Audio('./audio/success.wav');
let audio_fail = new Audio('./audio/fail.wav');
function init() {
document.getElementById('numberQuestionsTotal').innerHTML = questions.length;
showQuestions();
}
function showQuestions() {
if (gameOver()) {
showEndScreen();
} else {
showPlayScreen();
}
}
function gameOver(){
return currentQuestion >= questions.length
}
function answer(selection) {
let question = questions[currentQuestion];
let selectedQuestionNumber = selection.slice(-1);
let idOfRightAnswer = `answer${question['right-answer']}`
if (rightQuestinSelected(selectedQuestionNumber, question)) {
document.getElementById(selection).parentNode.classList.add('bg-success');
rightQuestions = rightQuestions + 1;
audio_success.play();
showRightAnsweredQuestions(rightQuestions);
} else {
document.getElementById(selection).parentNode.classList.add('bg-danger');
document.getElementById(idOfRightAnswer).parentNode.classList.add('bg-success');
audio_fail.play();
}
document.getElementById('next-button').disabled = false;
}
function rightQuestinSelected(selectedQuestionNumber, question){
return selectedQuestionNumber == question['right-answer']
}
function nextQuestion() {
currentQuestion++;
document.getElementById('next-button').disabled = true;
resetAnswerButtons();
showQuestions();
showQuestionNumber(currentQuestion);
}
function resetAnswerButtons() {
for (let i = 1; i < questions.length; i++) {
document.getElementById(`answer${i}`).parentNode.classList.remove('bg-success');
document.getElementById(`answer${i}`).parentNode.classList.remove('bg-danger');
}
}
function showQuestionNumber(index) {
document.getElementById('currentQuestionNumber').innerHTML = index + 1;
}
function showRightAnsweredQuestions(rightQuestions){
document.getElementById('rightAnswers').innerHTML = rightQuestions;
}
function startGame(){
currentQuestion = 0;
rightQuestions = 0;
document.getElementById('rightAnswers').innerHTML = rightQuestions;
document.getElementById('quizEnded').classList.add('d-none');
document.getElementById('quizStandard').classList.remove('d-none');
document.getElementById('progress').classList.remove('d-none');
document.getElementById('currentQuestionNumber').innerHTML = 1;
init();
}
function showEndScreen(){
document.getElementById('quizEnded').classList.remove('d-none');
document.getElementById('quizStandard').classList.add('d-none');
document.getElementById('totalAnswers').innerHTML = questions.length;
document.getElementById('progress').classList.add('d-none');
}
function showPlayScreen(){
calculatePercentage();
let question = questions[currentQuestion];
document.getElementById('question').innerHTML = question['question'];
document.getElementById('answer1').innerHTML = question['answer-1'];
document.getElementById('answer2').innerHTML = question['answer-2'];
document.getElementById('answer3').innerHTML = question['answer-3'];
document.getElementById('answer4').innerHTML = question['answer-4'];
}
function calculatePercentage(){
let percent = currentQuestion / questions.length;
// Math.round wird in diesem Beispiel nicht benötigt, aber wenn sich die Anzahl der Fragen ändern würde:
percent = Math.round( percent * 100);
document.getElementById('progress-bar').innerHTML = `${percent} %`
document.getElementById('progress-bar').style = `width: ${percent}%`
}