-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
104 lines (88 loc) · 3.41 KB
/
script.js
File metadata and controls
104 lines (88 loc) · 3.41 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
let questions = [];
let currentQuestion = 0;
let yourScore = 0;
let AUDIO_SUCCESS = new Audio('./audio/success.mp3');
let AUDIO_WRONG= new Audio('./audio/wrong.mp3');
function init() {
document.getElementById("background").classList.add("d-none");
document.getElementById("quiz-card").classList.remove("d-none");
document.getElementById("all-questions").innerHTML = questions.length;
showQuestion();
}
function showQuestion() {
if (gameIsOver()) {
showEndScreen();
} else {
updateProgressBar();
updateToNextQuestion();
}
}
function gameIsOver(){
return currentQuestion >= questions.length;
}
function answer(answer) {
let question = questions[currentQuestion];
let selectedAnswer = answer.slice(-1);
let idOfRightAnswer = `answer_${question.right_answer}`;
if (rightAnswerSelected(selectedAnswer, question)) {
document.getElementById(answer).classList.add("bg-success");
AUDIO_SUCCESS.play();
yourScore += 1;
} else {
document.getElementById(answer).classList.add("bg-danger");
document.getElementById(idOfRightAnswer).classList.add("bg-success");
AUDIO_WRONG.play();
}
document.getElementById("next-button").disabled = false;
}
function rightAnswerSelected(selectedAnswer, question){
return selectedAnswer == question.right_answer;
}
function nextQuestion() {
currentQuestion += 1; // or currentQuestion ++
document.getElementById("next-button").disabled = true;
resetAnswerButtons();
showQuestion();
}
function resetAnswerButtons() {
document.getElementById("answer_1").classList.remove("bg-success");
document.getElementById("answer_1").classList.remove("bg-danger");
document.getElementById("answer_2").classList.remove("bg-success");
document.getElementById("answer_2").classList.remove("bg-danger");
document.getElementById("answer_3").classList.remove("bg-success");
document.getElementById("answer_3").classList.remove("bg-danger");
document.getElementById("answer_4").classList.remove("bg-success");
document.getElementById("answer_4").classList.remove("bg-danger");
}
function pushData(category) {
questions = questions.concat(questionsData[category]);
}
function restartGame() {
questions = [];
currentQuestion = 0;
yourScore = 0;
console.log('hello')
document.getElementById("end-screen").classList.add("d-none");
document.getElementById("background").classList.remove("d-none");
}
function showEndScreen(){
document.getElementById("quiz-card").classList.add("d-none");
document.getElementById("end-screen").classList.remove("d-none");
document.getElementById("possible-score").innerHTML = questions.length;
document.getElementById("your-score").innerHTML = yourScore;
}
function updateToNextQuestion(){
let question = questions[currentQuestion];
document.getElementById("question-count").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 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}%;`;
}