-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
203 lines (166 loc) · 5.52 KB
/
app.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
const gameTitle = document.querySelector('.game h1');
const dataContainer = document.querySelector('.data_container');
const gameContent = document.querySelector('.game_content');
const btnRestart = document.querySelector('#btn_restart');
const playerScore = document.querySelector('#infos_score');
const timer = document.querySelector('#infos_timer');
const progressBarCount = document.querySelector('#progress_bar_count');
const innerProgressBar = document.querySelector('#progress_bar_inner');
const allCategories = document.querySelectorAll('.game_categorie button');
let questionIndex = 0;
let questionNumber = 1;
let score = 0;
let countDown = 10;
let timerId;
let selectedCategory = [];
//FETCH DATA
fetch('./data.json')
.then(res => {
if(!res.ok){
console.log(`error ${res.status}`);
}
else {
res.json().then(data => {
initChosenCategory(data);
})
}
});
//CHOOSE QUESTION CATEGORIE
function initChosenCategory(allQuestions){
let startIndex;
allCategories.forEach(categorie => {
categorie.addEventListener('click', () => {
switch(categorie.innerHTML){
case 'Films':
startIndex = 0;
break;
case 'Sports':
startIndex = 10;
break;
case 'Séries TV':
startIndex = 20;
break;
case 'Dessins animés':
startIndex = 30;
break;
case 'Musiques':
startIndex = 40;
break;
case 'Animaux':
startIndex = 50;
break;
case 'Jeux vidéos':
startIndex = 60;
break;
case 'Histoire':
startIndex = 70;
break;
case 'Années 80':
startIndex = 80;
break;
case 'Géographie':
startIndex = 90;
break;
}
if(startIndex !== undefined){
//TIMER
if(!timerId){
startTimer();
};
gameContent.style.display = 'flex';
document.querySelector('.game_categorie').style.display = 'none';
selectedCategory = allQuestions.splice(startIndex, 10);
showCategoryQuestions(selectedCategory);
};
})
})
};
//DISPLAY QUESTION
function showCategoryQuestions(selectedCategory){
const question = selectedCategory[questionIndex].question;
const answers = selectedCategory[questionIndex].options;
const correctAnswer = selectedCategory[questionIndex].answer;
const questionElement = document.createElement('h2');
questionElement.innerHTML = question;
dataContainer.appendChild(questionElement);
const answersElement = document.createElement('ul');
dataContainer.appendChild(answersElement);
answers.forEach(answer => {
const answerElement = document.createElement('li');
answerElement.classList.add('list_item');
answerElement.innerHTML = answer;
answersElement.appendChild(answerElement);
answerElement.addEventListener('mouseover', () => {
answerElement.classList.add('showHover');
});
answerElement.addEventListener('mouseout', () => {
answerElement.classList.remove('showHover');
});
checkAnswer(answerElement, correctAnswer);
})
};
//CHECK GOOD ANSWER AND DISPLAY SCORE
function checkAnswer(answers, correctAnswer){
answers.addEventListener('click', () => {
if(answers.innerHTML.includes(correctAnswer)){
answers.classList.add('correctAnswer');
score++;
} else {
answers.classList.add('wrongAnswer');
}
countDown = 11;
setTimeout(() => {
displayNextQuestion();
}, 200);
playerScore.innerHTML = `${score}`;
//SHOW RESTART BUTTON
if(questionNumber >= 10){
setTimeout(() => {
gameTitle.innerHTML = 'Game Over';
btnRestart.style.display = 'block';
}, 200);
}
});
};
//DISPLAY NEW QUESTION
function displayNextQuestion() {
questionIndex++;
questionNumber++;
if(questionNumber <= 10){
updateProgressBar()
} else {
clearInterval(timerId);
timer.classList.remove('timerPulse');
};
const questionElement = document.querySelector('h2');
const answersElement = document.querySelector('ul');
questionElement.remove();
answersElement.remove();
if (questionIndex < selectedCategory.length) {
showCategoryQuestions(selectedCategory);
};
};
//PROGRESS BAR
function updateProgressBar(){
if(questionNumber <= 10){
progressBarCount.innerHTML = `${questionNumber} / 10`;
innerProgressBar.style.width = ` ${(questionNumber * 10)}%`;
};
};
//START TIMER
function startTimer(){
timerId = setInterval(() => {
if(countDown > 0){
countDown--;
} else {
displayNextQuestion();
countDown = 10;
}
countDown <= 5 ? timer.classList.add('timerPulse') : timer.classList.remove('timerPulse');
countDown < 10 ? timer.innerHTML = `0${countDown}` : timer.innerHTML = `${countDown}`;
}, 1000);
}
//RESTART GAME
btnRestart.addEventListener('click', () => {
window.location.reload();
});