-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
244 lines (171 loc) · 5.92 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const domOn = (selector, event, callback) => {
document.querySelectorAll(selector).forEach(el => el.addEventListener(event, callback))
}
const win = (score) => {
let rang = ""
if (score < 10 ) {
rang = "SILVER"
} else if (score < 20) {
rang = "GOLD"
}else if (score < 38) {
rang = "PLATINE"
} else {
rang = "MASTER"
}
return rang
}
localStorage.setItem('question', 0);
localStorage.setItem('total', 0);
const TEMP_QUESTION = document.querySelector(".question");
const TEMP_RESPONSE = document.querySelector(".response");
const finish = new Event("finish")
const test = [
{
question: "Tu es plutôt ... ",
response: [
{
script: "GIFTBOOSTER",
point: 10
},
{
script: "GHOSTBUSTER",
point: 5
},
]
},
{
question: "Combien de commerçants existent dans la Suisse romande ? ",
response: [
{
script: "Aucun, je suis survivaliste ",
point: 3
},
{
script: "1 à 10’000 ",
point: 20
},
{
script: "10’001 à 100’000 ",
point: 1
},
{
script: "Je n’ai pas assez de doigts pour tous les compter ",
point: 10
},
]
},
{
question: "« BOOSTER » pour toi, c’est quoi ? ",
response: [
{
script: "Amplificateur ",
point: 5
},
{
script: "Réducteur ",
point: 0
},
{
script: "Accélérateur ",
point: 10
},
{
script: "C’est pas une carte Pokémon ? ",
point: 10
},
]
},
{
question: "Tu préfères aider une personne qui en a besoin, ou profiter d’une super offre proposée par un commerçant de ta région?",
response: [
{
script: "Aider",
point: 5
},
{
script: "Profiter",
point: 0
},
{
script: "J'aimerais qu'une platerforme me donne la possibilité de faire les deux ",
point: 10
},
]
},
]
domOn("#begin", "click", async (evt) => {
document.querySelector("#startPage").classList.add("hidden")
document.querySelector("#quizz").classList.remove("hidden")
let nbquestion = localStorage.getItem("question")
TEMP_QUESTION.textContent = test[nbquestion].question;
document.querySelectorAll(".response").forEach(el => el.remove())
for (const response of test[nbquestion].response) {
let cloneResponse = TEMP_RESPONSE.cloneNode(true)
cloneResponse.textContent = response.script
cloneResponse.classList.remove("blue")
cloneResponse.setAttribute('data-point', response.point)
document.querySelector("#globalresponse").appendChild(cloneResponse)
}
localStorage.setItem("question", parseInt(nbquestion) + 1)
})
domOn("#next", "click", async (evt) => {
let givenresponses = document.querySelectorAll(".response");
for (const iterator of givenresponses) {
if (parseInt(iterator.getAttribute("data-bool")) === 1) {
localStorage.setItem("total", (parseInt(localStorage.getItem("total")) + parseInt(iterator.getAttribute("data-point"))))
}
}
let nbquestion = localStorage.getItem("question")
if (nbquestion > Object.keys(test).length - 1) {
return document.dispatchEvent(finish);
}
document.querySelectorAll(".response").forEach(el => el.remove())
TEMP_QUESTION.textContent = test[nbquestion].question;
for (const response of test[nbquestion].response) {
let cloneResponse = TEMP_RESPONSE.cloneNode(true)
cloneResponse.textContent = response.script
cloneResponse.classList.remove("blue")
cloneResponse.setAttribute('data-point', response.point)
document.querySelector("#globalresponse").appendChild(cloneResponse)
}
localStorage.setItem("question", parseInt(nbquestion) + 1)
})
domOn("#globalresponse", "click", async (evt) => {
const target = evt.target;
if (!target.classList.contains('response')) return;
if (!target.classList.contains('blue')) {
document.querySelectorAll(".response").forEach(el => {
el.classList.remove("blue")
el.setAttribute('data-bool', 0)
})
target.classList.add("blue")
target.setAttribute('data-bool', 1)
}
})
document.addEventListener("finish", async (evt) => {
document.querySelector("#startPage").classList.add("hidden")
document.querySelector("#quizz").classList.add("hidden")
document.querySelector("#end").classList.remove("hidden")
const grade = win(localStorage.getItem("total"))
document.querySelector("#number").textContent = localStorage.getItem("total")
document.querySelector("#level").textContent = grade
document.getElementById("stringmail").focus();
})
domOn("#submitemail", "click", async (evt) => {
const email = document.querySelector("#stringmail").value
if (email.length > 0) {
try {
const response = await fetch("https://calm-headland-16602.herokuapp.com/", {
method: "POST",
headers: new Headers({
"Content-Type": "application/json",
}),
body: JSON.stringify({ email: email })
})
} catch (err) {
return console.log(err)
}
document.querySelector("#email").remove();
document.querySelector("#congrats").textContent = "Un tout GRAND Merci pour ta participation ! "
}
})