-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
117 lines (71 loc) · 3.2 KB
/
app.js
File metadata and controls
117 lines (71 loc) · 3.2 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
let NUMBER_GOOD_QUESTIONS = 0;
let NUMBER_BAD_QUESTIONS = 0;
async function getQuestions() {
const response = await fetch('./questions.json');
return await response.json();
}
function createQuestions(question, numQuestion) {
// Div principale - htmlInputQuestion
let htmlInputQuestion = document.createElement("div");
htmlInputQuestion.setAttribute("class","inputQuestion");
htmlInputQuestion.setAttribute("id",question[numQuestion].questionId);
// Titre de la question
let htmlInputQuestionTitle = document.createElement("p");
htmlInputQuestionTitle.setAttribute("class", "inputQuestionTitle");
htmlInputQuestionTitle.textContent = question[numQuestion].questionTitle;
htmlInputQuestion.appendChild(htmlInputQuestionTitle);
// Réponses potentielles de la question
for (let i = 0; i < question[numQuestion].questionResponses.length; i++) {
let htmlRep = document.createElement("input");
htmlRep.setAttribute("type", "radio");
htmlRep.setAttribute("id", question[numQuestion].questionResponses[i].responseId);
htmlRep.setAttribute("name", question[numQuestion].questionId);
htmlInputQuestion.appendChild(htmlRep);
let htmlLabel = document.createElement("label");
htmlLabel.setAttribute("for", question[numQuestion].questionResponses[i].responseId);
htmlLabel.setAttribute("class", "labelResponse");
htmlLabel.textContent = " " + question[numQuestion].questionResponses[i].responseContent + '\n';
htmlInputQuestion.appendChild(htmlLabel);
}
document.body.insertBefore(htmlInputQuestion, document.getElementById("enterButton"));
}
function goodResponse(idElement) {
NUMBER_GOOD_QUESTIONS++;
let currentElement = document.getElementById(idElement);
console.log(currentElement);
currentElement.setAttribute("class", "goodResponse");
}
function badQuestion(idElement) {
NUMBER_BAD_QUESTIONS++;
console.log(idElement);
let currentElement = document.getElementById(idElement);
currentElement.setAttribute("class", "badResponse");
}
getQuestions().then(r => {
const { numberOfQuestions, questions } = r;
//console.log(questions[1].questionId);
for (let i = 0; i < numberOfQuestions; i++) {
createQuestions(questions, i);
}
});
document.getElementById("enterButton").addEventListener("click", function() {
// Récupérer les résultats de réponses
getQuestions().then(r => {
const { numberOfQuestions, questions } = r;
for (let i = 0; i < questions.length; i++) {
let questionResponses = questions[i].questionResponses;
for (let j = 0; j < questionResponses.length; j++) {
//console.log(questionResponses[j]);
if (questionResponses[j].goodResponse === true) {
if (document.getElementById(questionResponses[j].responseId).checked) {
console.log("Bonne réponse");
goodResponse(questions[i].questionId);
} else {
badQuestion(questions[i].questionId);
}
}
}
}
})
})
// Valider les questions.