-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
171 lines (137 loc) · 6.42 KB
/
app.js
File metadata and controls
171 lines (137 loc) · 6.42 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
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
const questions = [
{
id: 1, question: "A quel écrivain doit-on le personnage de Boule-de-Suif ?",
reponses: { a: "Guy de Maupassant", b: "Victor Hugo", c: "Honoré de Balzac" },
correct: "Guy de Maupassant"
},
{
id: 2, question: "Quel conseil régional est présidé par Ségolène Royal depuis 2004 ?",
reponses: { a: "Charentes-maritime", b: "Poitou-Charentes", c: "Haut-de-France" },
correct: "Poitou-Charentes"
},
{
id: 3, question: "De quel pays Tirana est-elle la capitale ?",
reponses: { a: "le Kossovo", b: "L'Albanie", c: "L'Ukraine" },
correct: "L'Albanie"
},
{
id: 4, question: "En géométrie, combien de côtés possède un losange ?",
reponses: { a: "7", b: "3", c: "4" },
correct: "4"
},
{
id: 5, question: "Quel est l'impératif du verbe peindre à la 2e personne du pluriel ?",
reponses: { a: "Pegnez", b: "Peigniez", c: "Peignez" },
correct: "Peignez"
}
];
// ButtonS selector
const centerSide = document.querySelector('.center-side')
const topInside = document.querySelector('.top-inside')
const laQuestion = document.querySelector('.lesQuestions')
const topResult = document.querySelector('.top-result')
const goodResponse = document.querySelector('.good-response')
const badResponse = document.querySelector('.bad-response')
const responseChoice = document.querySelector('.response-choice')
const btnModal = document.querySelector('.modal')
const btnClose = document.querySelector('.close-btn')
const reponsesQuestions = document.querySelector('.responses')
const bonneReponseSelectionner = document.querySelector('.selection')
const scorePoint = document.querySelector('.score')
//Button next and prev
const BtnNext = document.querySelector('.btn-next')
const BtnPrev = document.querySelector('.btn-prev')
//BUTTON EVENTS LISTENER
BtnNext.addEventListener('click', nextQuestion)
BtnPrev.addEventListener('click', previewQuestion)
//list.addEventListener('click', choisirReponse)
reponsesQuestions.addEventListener('click', showGoodResponse)
//EVENTS LISTENER
window.addEventListener('DOMContentLoaded', loadQuestions)
let currentQuestions = 0
let bonneReponse = 0
let bonneAnswer = 0
//FUNCTION LOAD QUESTIONS
function loadQuestions() {
showQuestions(currentQuestions)
showGoodResponse(bonneReponse)
}
//FUNCTION SHOW QUESTIONS
function showQuestions(question) {
const item = questions[question]
topInside.textContent = `Question N° ${item.id}/${questions.length}`
laQuestion.textContent = item.question
bonneReponseSelectionner.textContent = "Selectionnez votre réponse"
const response = []
for (const letter in item.reponses) {
response.push(
`<button type="button" class="reponses">
${item.reponses[letter]}
</button>`
)
reponsesQuestions.innerHTML = response.join("")
}
}
//GET RESPONSES
function showGoodResponse(reponse) {
const bnreponse = questions[reponse]
const paragraf = reponsesQuestions.querySelectorAll('button')
paragraf.forEach(data => {
data.addEventListener('click', (e) => {
data.style.pointerEvents= 'none'
const targetP = e.currentTarget.textContent.trim()
if (targetP === bnreponse.correct) {
bonneAnswer++
goodResponse.style.display = "block"
goodResponse.classList.add('good-response')
data.style.backgroundColor = "green"
data.style.color = "white"
setTimeout(() => {
goodResponse.style.display = "none"
}, 1000)
disableBtn()
}
if (targetP !== bnreponse.correct) {
bonneAnswer += 0
badResponse.style.display = "block"
badResponse.classList.add('bad-response')
data.style.backgroundColor = "red"
data.style.color = "white"
bonneReponseSelectionner.innerHTML = `La bonne réponse est: <span class="reponseGood">${bnreponse.correct}</span> `
setTimeout(() => {
badResponse.style.display = "none"
}, 1000)
disableBtn()
}
})
})
scorePoint.innerHTML = `<h4>SCORE: ${bonneAnswer}/${questions.length}</h4>`
}
// DISABLE OTHERS BUTTONS AFTER STARTING THE QUIZZ
function disableBtn() {
const btnDisabled= reponsesQuestions.querySelectorAll('button')
btnDisabled.forEach(btn=>{
btn.style.pointerEvents= 'none'
})
}
//BUTTON NEXT AND PREVIEW FUNCTION
function nextQuestion() {
currentQuestions++
bonneReponse++
if (currentQuestions > questions.length - 1) {
currentQuestions = 0
bonneAnswer = 0
bonneReponse= 0
}
showQuestions(currentQuestions)
showGoodResponse(bonneReponse)
}
function previewQuestion() {
currentQuestions--
bonneReponse--
if (currentQuestions < 0) {
currentQuestions = questions.length - 1
}
showQuestions(currentQuestions)
showGoodResponse(bonneReponse)
}