-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
122 lines (106 loc) · 4.13 KB
/
Copy pathscript.js
File metadata and controls
122 lines (106 loc) · 4.13 KB
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
let currentQuestion = 0;
let rightAnswerCounter = 0;
let audioSuccess = new Audio('audio/success.mp3');
let audioWrong = new Audio('audio/wrong.mp3');
function init() {
showQuestionsLength();
showQuestion();
}
function showQuestionsLength() { //Shows total amount of questions
document.getElementById('questionLength').innerHTML = questions.length;
}
function showQuestion() { //manages the game content
if (gameIsOver()) {
//show End Screen
showEndScreen();
} else { // Show question
updateToNextQuestion();
updateProgressbar();
}
}
function disableAllAnswers() { //disable all buttons after input
for (let i = 1; i < 5; i++) {
document.getElementById(`answer${i}Button`).disabled = true;
}
}
function enableAnswerButtons() { //enable all buttons after heading to next question
for (let i = 1; i < 5; i++) {
document.getElementById(`answer${i}Button`).disabled = false;
}
}
function answer(selection) {
let question = questions[currentQuestion];
let answerNumber = selection.slice(-1);
let idOfRightAnswer = `answer${question['right_answer']}`;
if (rightAnswerSelected(answerNumber, question)) { //right answer
rightAnswerProcess(selection);
disableAllAnswers();
} else { //answer is wrong
wrongAnswerProcess(selection, idOfRightAnswer);
disableAllAnswers();
}
document.getElementById('nextButton').disabled = false;
}
function rightAnswerProcess(selection) {
document.getElementById(selection).parentNode.classList.add('bg-success');
rightAnswerCounter++
audioSuccess.play();
}
function wrongAnswerProcess(selection, idOfRightAnswer) {
document.getElementById(selection).parentNode.classList.add('bg-danger');
document.getElementById(idOfRightAnswer).parentNode.classList.add('bg-success');
audioWrong.play();
}
function rightAnswerSelected(answerNumber, question) {
return answerNumber == question['right_answer'];
}
function updateProgressbar() {
let percent = (currentQuestion + 1) / questions.length;
percent = Math.round(percent * 100);
console.log('Fortschritt', percent);
document.getElementById('progressBar').innerHTML = `${percent}%`;
document.getElementById('progressBar').style = `width: ${percent}%;`;
}
function updateToNextQuestion() {
let question = questions[currentQuestion];
document.getElementById('currentQuestionNumber').innerHTML = currentQuestion + 1;
let questionText = document.getElementById('questionText');
document.getElementById('answer1').innerHTML = question['answer1'];
document.getElementById('answer2').innerHTML = question['answer2'];
document.getElementById('answer3').innerHTML = question['answer3'];
document.getElementById('answer4').innerHTML = question['answer4'];
questionText.innerHTML = `${question['question']}`;
}
function nextQuestion() {
currentQuestion++;
document.getElementById('nextButton').disabled = true;
showQuestionsLength();
resetAnswerButtons();
showQuestion();
enableAnswerButtons();
}
function gameIsOver() {
return currentQuestion >= questions.length;
}
function resetAnswerButtons() { //adding green and red bg after answer input
for (i = 1; i < 5; i++) {
document.getElementById('answer' + i).parentNode.classList.remove('bg-success');
document.getElementById('answer' + i).parentNode.classList.remove('bg-danger');
}
}
function showEndScreen() {
document.getElementById('endScreen').style = '';
document.getElementById('questionBody').style = 'display: none;';
document.getElementById('amountOfQuestions').innerHTML = rightAnswerCounter;
document.getElementById('countOfAllAnswers').innerHTML = questions.length;
document.getElementById('headerImage').src = 'img/winner.png';
document.getElementById('result').innerHTML = '<img src="img/brain result.png">';
}
function restartGame() {
document.getElementById('headerImage').src = "img/question.jpg";
document.getElementById('questionBody').style = '';
document.getElementById('endScreen').style = 'display: none;';
currentQuestion = 0;
rightAnswerCounter = 0;
init();
}