forked from Molerog/Proyecto-SPA--Quizz-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
237 lines (210 loc) · 7.51 KB
/
main.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
//We take consts from DOM
const textWelcome = document.getElementById("textWelcome");
const questionContainer = document.getElementById("questionContainer");
const resultContainer = document.getElementById("resultContainer");
const startButton = document.getElementById("start-btn")
const nextButton = document.getElementById("next-btn");
const restartButton = document.getElementById("restart-btn");
const questionPrint = document.getElementById("questionPrint")
const answerButtonPrint = document.getElementById("buttonContainer");
const showScore = document.getElementById ('showScore');
const globalQuestion = document.getElementById ('globalQuestion');
const scoreText = document.getElementById ('scoreText');
let modal = document.getElementById("myModal");
const btn2 = document.getElementById("viewStats");
let span = document.getElementsByClassName("close")[0];
const myBar = document.getElementById("myBar");
//We create variables for counters
const totalQuestions = 10;
let correctAnswers = 0;
let currentQuestionIndex;
let currentGame = 0;
totalGames = []
totalCorrects=[]
let myChart;
//Here we trigger the Start Button to begin the game
startButton.addEventListener('click',() => {
localStorage.removeItem('counter')
correctAnswers = 0;
startGame()
})
//Event on next button to set and count the next question
nextButton.addEventListener('click',() =>{
currentQuestionIndex++;
setNextQuestion()
})
//Event on restart button in order to restart game and remove counter from local storage
restartButton.addEventListener('click', () =>{
localStorage.removeItem('counter')
correctAnswers = 0;
startGame()
})
//Event that triggers the function to see your stats
showScore.addEventListener('click', showResults)
// When the user clicks the button, open the modal
btn2.addEventListener('click',function() {
modal.style.display = "block";
})
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
//Function so start game
function startGame(){
currentGame ++;
totalGames.push(currentGame)
const data = {
labels: totalGames,
datasets: [{
label: 'Your progress',
backgroundColor: 'rgb(51, 161, 253)',
borderColor: 'rgb(255, 99, 132)',
data: totalCorrects,
}]
};
const config = {
type: 'bar',
data: data,
};
if (myChart) {
myChart.destroy();
}
myChart = new Chart('chart', config);
axios.get("https://opentdb.com/api.php?amount=10&type=multiple")
.then((res) => {
currentQuestionIndex = 0
let arrayQuestions = res.data.results
textWelcome.classList.add('hide')
globalQuestion.classList.remove('hide')
resultContainer.classList.add('hide')
showScore.classList.add('hide')
localStorage.setItem('preguntas', JSON.stringify(arrayQuestions))
setInitialQuestion(arrayQuestions)
})
.catch((err) => console.error(err));
}
//Function to set the initial question
function setInitialQuestion(arrayQuestions){
resetState()
showQuestion(arrayQuestions[currentQuestionIndex])
}
//Function to reset the the answers state before print the next one
function resetState() {
nextButton.classList.add('hide');
while (answerButtonPrint.firstChild){
answerButtonPrint.removeChild(answerButtonPrint.firstChild)
}
}
//Function to print the current question on HTML
function showQuestion(question){
myBar.style.width = `${(currentQuestionIndex/totalQuestions) * 100}%`
questionPrint.innerHTML = `<h2>${question.question}</h2>`;
showAnswers(question)
}
//Function to show the answers
function showAnswers(question){
let filterAnswer = question.correct_answer.replaceAll(/"/g, "'").replaceAll(/é/g, "é").replaceAll(/'/g, " ").replaceAll(/&uoml;/g, "").replaceAll(/®/g, "").replaceAll(/&/g, "")
let correctAnswer = {
text : filterAnswer,
correct : true
};
let answers =[]
let incorrectAnswer = question.incorrect_answers;
incorrectAnswer.forEach(incorrect => {
let incorrectFormat = incorrect.replaceAll(/"/g, "'").replaceAll(/é/g, "é").replaceAll(/'/g, " ").replaceAll(/&uoml;/g, "").replaceAll(/®/g, "").replaceAll(/&/g, "")
answers.push({text : incorrectFormat, correct : false})
})
answers.push(correctAnswer)
answers.sort(function(a,b){
if (a.text > b.text){
return 1
}
if (a.text < b.text){
return -1
}
return 0
})
answers.forEach(answer => {
const button = document.createElement("button")
button.innerText = answer.text
button.classList.add("button_answer")
if (answer.correct){
button.dataset.correct = true
}
button.addEventListener('click',() =>{
if (answer.correct == true){
correctAnswers++;
localStorage.setItem('counter', JSON.stringify(correctAnswers));
}
localStorage.setItem('counter', JSON.stringify(correctAnswers));
selectAnswer();
})
answerButtonPrint.appendChild(button);
})
}
//Funciton to detect the correct and wrong answers
function selectAnswer(){
Array.from(answerButtonPrint.children).forEach((button) => {
button.disabled = true;
setStatusClass(button, button.dataset.correct);
});
if ( totalQuestions > currentQuestionIndex + 1) {
nextButton.classList.remove('hide');
} else{
totalCorrects.push(correctAnswers)
showScore.classList.remove('hide');
}
}
//Function that set the color to the buttons as needed
function setStatusClass(element, correct){
if (correct){
element.classList.add('correct');
} else {
element.classList.add('wrong');
}
}
//function to set the second and the incoming questions
function setNextQuestion(){
let preguntas = JSON.parse(localStorage.getItem('preguntas'))
resetState();
showQuestion(preguntas[currentQuestionIndex]);
}
//Function to print your stats
function showResults(){
globalQuestion.classList.add('hide');
resultContainer.classList.remove('hide');
showScore.classList.remove('hide');
let counter = JSON.parse(localStorage.getItem('counter'))
if (counter == 0 || counter <= 2) {
scoreText.innerHTML = `<div class= "circle"> <h2 class="textResult"><span class="decorationScore"> ${counter} / 10 </span> <br> LOOSER </h2> </div>
<audio autoplay>
<source src="./Assets/Fail.mp3" type="audio/mpeg">
</audio>`;
} else if (counter >2 && counter < 5) {
scoreText.innerHTML = `<div class= "circle"> <h2 class="textResult"> <span class="decorationScore"> ${counter} / 10 </span> <br> YOU ALMOST GOT IT!</div> </h2>
<audio autoplay>
<source src="./Assets/Retratado.mp3" type="audio/mpeg">
</audio>`;
} else if (counter >= 5 && counter <=7) {
scoreText.innerHTML = `<div class= "circle"> <h2 class="textResult"> <span class="decorationScore"> ${counter} / 10 </span> <br> YOU'RE DOING GREAT! </div> </h2>
<audio autoplay>
<source src="./Assets/Kids.mp3" type="audio/mpeg">
</audio>`;
} else if (counter >7 && counter < 10) {
scoreText.innerHTML = `<div class= "circle"> <h2 class="textResult"> <span class="decorationScore"> ${counter} / 10 </span> <br> YOU ARE A BEAST! </div> </h2>
<audio autoplay>
<source src="./Assets/Chiquito.mp3" type="audio/mpeg">
</audio>`;
} else {
scoreText.innerHTML = `<div class= "circle"> <h2 class="textResult"> <span class="decorationScore"> ${counter} / 10 </span> <br> LEGEND! </div> </h2>
<audio autoplay>
<source src="./Assets/Austin.mp3" type="audio/mpeg">
</audio>`;
}
}