-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
147 lines (118 loc) · 3.55 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
import "./style.css";
import { Questions } from "./questions";
console.log(Questions);
const app = document.querySelector("#app");
const buttonStart = document.querySelector("#start");
buttonStart?.addEventListener("click", startQuiz);
function startQuiz(event) {
event.stopPropagation();
let currentQuestions = 0;
let score = 0;
clean();
displayQuestion(currentQuestions);
function clean() {
while (app?.firstElementChild) {
app.firstElementChild.remove();
}
}
function displayQuestion(index) {
const questions = Questions[index];
if (!questions) {
//finir le quiz
showDisplayFinish();
return;
}
const title = getTitleElement(questions.question);
app?.appendChild(title);
const answerDiv = createAnswer(questions.answers);
app?.appendChild(answerDiv);
const submittButton = getSubmitButton();
submittButton.addEventListener("click", submit);
app?.appendChild(submittButton);
function showDisplayFinish() {
const h1 = document.createElement("h1");
h1.innerText = "Bravo la partie est terminee";
const p = document.createElement("p");
p.innerText = `Tu as obtenue ${score} sur ${Questions.length} point`;
app?.appendChild(h1);
app?.appendChild(p);
}
}
function submit() {
const selectedAnswer = document.querySelector(
'input[name="answer"]:checked'
);
const value = selectedAnswer?.value;
const questions = Questions[currentQuestions];
const isCorrect = questions.correct === value;
if (isCorrect) {
score++;
}
showFeedback(isCorrect, questions.correct, value);
const feedback = getFeedbagPhrase(isCorrect, questions.correct);
app?.appendChild(feedback);
setTimeout(() => {
currentQuestions++;
clean();
displayQuestion(currentQuestions);
}, 2000);
}
function createAnswer(answers) {
const answerDiv = document.createElement("div");
answerDiv.classList.add("answers");
for (const answer of answers) {
const label = getAnswerElement(answer);
answerDiv.appendChild(label);
}
return answerDiv;
}
}
function formatId(text) {
return text.replaceAll(" ", "-").toLowerCase();
}
function getTitleElement(text) {
const title = document.createElement("h3");
title.innerText = text;
return title;
}
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 getSubmitButton() {
const submit = document.createElement("button");
submit.innerText = "submit";
return submit;
}
function showFeedback(isCorrect, correct, answer) {
const correctAnswer = formatId(correct);
const correctElement = document.querySelector(
`label[for="${correctAnswer}"]`
);
const selectedAnswer = formatId(answer);
const selectedElemen = document.querySelector(
`label[for="${selectedAnswer}"]`
);
if (isCorrect) {
selectedElemen?.classList.add("correct");
} else {
selectedElemen?.classList.add("incorrect");
correctElement?.classList.add("correct");
}
}
function getFeedbagPhrase(isCorrect, correct) {
const paragraph = document.createElement("p");
paragraph.innerText = isCorrect
? "Bravo tu as trouver la bonne reponse "
: `oups... la bonne reponse est ${correct}`;
return paragraph;
}