-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
179 lines (145 loc) · 4.57 KB
/
main.js
File metadata and controls
179 lines (145 loc) · 4.57 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
170
171
172
173
174
175
176
177
178
179
import "./style.css";
import { Questions } from "./questions";
const TIMEOUT = 4000;
const app = document.getElementById("app");
const startButton = document.querySelector("#start");
startButton.addEventListener("click", startQuiz);
function startQuiz(event) {
event.stopPropagation();
let currentQuestion = 0;
let score = 0;
displayQuestion(currentQuestion);
function clean() {
while (app.firstElementChild) {
app.firstElementChild.remove();
}
const progress = displayProgressBar(Questions.length, currentQuestion);
app.appendChild(progress);
}
function displayQuestion(index) {
clean();
const question = Questions[index];
if (!question) {
displayFinishMessage();
return;
}
const title = getTitleElement(question.question);
app.appendChild(title);
const answersDiv = createAnswers(question.answers);
app.appendChild(answersDiv);
const submitButton = document.createElement("button");
submitButton.innerText = "Envoyer";
app.appendChild(submitButton);
submitButton.addEventListener("click", submit);
}
function displayFinishMessage() {
const h1 = document.createElement("h1");
h1.innerText = "Bravo tu as terminé le quiz.";
const p = document.createElement("p");
p.innerText = `Tu as eu ${score} sur ${Questions.length} points ! `;
app.appendChild(h1);
app.appendChild(p);
}
function submit() {
const selectedAnswer = app.querySelector('input[name="answer"]:checked');
disableAnswers();
const value = selectedAnswer.value;
const question = Questions[currentQuestion];
const isCorrect = question.correct === value;
if (isCorrect) {
score++;
}
showFeedBack(isCorrect, question.correct, value);
displayNextQuestionButton(() => {
currentQuestion++;
displayQuestion(currentQuestion);
});
const feedBack = getFeedBackMessage(isCorrect, question.correct);
app.appendChild(feedBack);
}
function createAnswers(answers) {
const answersDiv = document.createElement("div");
answersDiv.classList.add("answers");
for (const answer of answers) {
const label = getAnswerElement(answer);
answersDiv.appendChild(label);
}
return answersDiv;
}
}
function getTitleElement(text) {
const title = document.createElement("h3");
title.innerText = text;
return title;
}
function formatId(text) {
return text.replaceAll(" ", "-").toLowerCase();
}
function getAnswerElement(text) {
const label = document.createElement("label");
label.innerText = text;
const input = document.createElement("input");
const id = formatId(text);
input.id = id;
label.htmlFor = id;
input.setAttribute("type", "radio");
input.setAttribute("name", "answer");
input.setAttribute("value", text);
label.appendChild(input);
return label;
}
function showFeedBack(isCorrect, correct, answer) {
const correctAnswerId = formatId(correct);
const correctElement = document.querySelector(
`label[for='${correctAnswerId}']`
);
const selectedAnswerId = formatId(answer);
const selectedElement = document.querySelector(
`label[for='${selectedAnswerId}']`
);
correctElement.classList.add("correct");
selectedElement.classList.add(isCorrect ? "correct" : "incorrect");
}
function getFeedBackMessage(isCorrect, correct) {
const paragraph = document.createElement("p");
paragraph.innerText = isCorrect
? "Bravo tu as eu la bonne reponse"
: `Desolé... mais la bonne reponse etait ${correct} `;
return paragraph;
}
function displayProgressBar(max, value) {
const progress = document.createElement("progress");
progress.setAttribute("max", max);
progress.setAttribute("value", value);
return progress;
}
function displayNextQuestionButton(callback) {
let remainingtimeout = TIMEOUT;
app.querySelector("button").remove();
const getButtonText = () =>
`Question suivante dans ${remainingtimeout / 1000}s`;
const nextButton = document.createElement("button");
nextButton.innerText = getButtonText();
app.appendChild(nextButton);
const interval = setInterval(() => {
remainingtimeout -= 1000;
nextButton.innerText = getButtonText();
}, 1000);
const handleNextQuestion = () => {
clearInterval(interval);
clearTimeout(timeout);
callback();
};
const timeout = setTimeout(() => {
handleNextQuestion();
}, TIMEOUT);
nextButton.addEventListener("click", () => {
handleNextQuestion();
});
}
function disableAnswers() {
const radioInputs = document.querySelectorAll('input[type="radio"]');
for (const radio of radioInputs) {
radio.disabled = true;
}
}