Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
322 lines (279 loc) · 6.78 KB
/
Copy pathscript.js
File metadata and controls
322 lines (279 loc) · 6.78 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Declaração de variáveis
const question = document.querySelector("#question");
const answersBox = document.querySelector("#answers-box");
const quizzContainer = document.querySelector("#quizz-container");
const scoreContainer = document.querySelector("#score-container");
const letters = ['a', 'b', 'c', 'd'];
let points = 0;
let actualQuestion = 0;
// Perguntas
const questions = [
{
"question": "PHP foi desenvolvido para qual fim?",
"answers": [
{
"answer": "back-end",
"correct": true
},
{
"answer": "front-end",
"correct": false
},
{
"answer": "Sistema operacional",
"correct": false
},
{
"answer": "Banco de dados",
"correct": false
},
]
},
{
"question": "Uma forma de declarar variável em JavaScript:",
"answers": [
{
"answer": "$var",
"correct": false
},
{
"answer": "var",
"correct": true
},
{
"answer": "@var",
"correct": false
},
{
"answer": "#let",
"correct": false
},
]
},
{
"question": "Qual o seletor de id no CSS?",
"answers": [
{
"answer": "#",
"correct": true
},
{
"answer": ".",
"correct": false
},
{
"answer": "@",
"correct": false
},
{
"answer": "/",
"correct": false
},
]
},
{
"question": "Qual é o operador lógico (And) em muitas linguagens de programação?",
"answers": [
{
"answer": "&&",
"correct": true
},
{
"answer": "||",
"correct": false
},
{
"answer": "!",
"correct": false
},
{
"answer": "==",
"correct": false
},
]
},
{
"question": "Qual é a estrutura de dados que segue o princípio ultimo a entrar, primeiro a sair?",
"answers": [
{
"answer": "Pilha(Stack)",
"correct": true
},
{
"answer": "Fila(Queue)",
"correct": false
},
{
"answer": "Lista ligada(Linked list)",
"correct": false
},
{
"answer": "Arvore(Tree)",
"correct": false
},
]
},
{
"question": "O que CSS significa?",
"answers": [
{
"answer": "Computer Style Sheets",
"correct": false
},
{
"answer": "Creative Style Sheets",
"correct": false
},
{
"answer": "Cascading Style Sheets",
"correct": true
},
{
"answer": "Colorful Style Sheets",
"correct": false
},
]
},
{
"question": "Qual linguagem é conhecida por ser uma linguagem de script do lado do cliente?",
"answers": [
{
"answer": "Java",
"correct": false
},
{
"answer": "Python",
"correct": false
},
{
"answer": "JavaScript",
"correct": true
},
{
"answer": "C++",
"correct": false
},
]
},
{
"question": "O que HTML representa?",
"answers": [
{
"answer": "Hyper Text Markup Language",
"correct": true
},
{
"answer": "High Tech Modern Language",
"correct": false
},
{
"answer": "Hyperlink and Text Markeup language",
"correct": false
},
{
"answer": "Home ToolMarku language",
"correct": false
},
]
},
]
// Substituição do layout pela primeira questão
function init() {
createQuestion(0)
}
// Create a question
function createQuestion(i) {
// Limpa questão anterior
const oldButtons = answersBox.querySelectorAll("button");
oldButtons.forEach(function(btn) {
btn.remove();
});
// Altera texto da pergunta
const questionText = question.querySelector("#question-text");
const questionNumber = question.querySelector("#question-number");
questionText.textContent = questions[i].question;
questionNumber.textContent = i + 1;
// Insere alternativas
questions[i].answers.forEach(function(answer, i) {
// Altera texto do template
const answerTemplate = document.querySelector(".answer-template").cloneNode(true);
const letterBtn = answerTemplate.querySelector(".btn-letter");
const answerText = answerTemplate.querySelector(".question-answer");
letterBtn.textContent = letters[i];
answerText.textContent = answer['answer'];
answerTemplate.setAttribute("correct-answer", answer["correct"]);
// remove classe de hide e template do template
answerTemplate.classList.remove("hide");
answerTemplate.classList.remove("answer-template");
// Insere template na tela
answersBox.appendChild(answerTemplate);
});
// Cria evento em todos os botões
const buttons = answersBox.querySelectorAll("button");
buttons.forEach(function(button) {
button.addEventListener("click", function() {
checkAnswer(this, buttons);
});
});
// Incrementa o número atual de questões
actualQuestion++;
}
// Verificando se resposta está correta
function checkAnswer(btn, buttons) {
// Exibir respostas erradas e a certa
buttons.forEach(function(button) {
if(button.getAttribute("correct-answer") === "true") {
button.classList.add("correct-answer");
// checa se o usuário acertou
if(btn === button) {
// incrementa os pontos
points++;
}
} else {
button.classList.add("wrong-answer");
}
});
nextQuestion();
}
// Exibe a próxima pergunta
function nextQuestion() {
// Timer para ver se acertou ou errou
setTimeout(function() {
// checa se ainda há mais perguntas
if(actualQuestion >= questions.length) {
// apresenta msg de sucesso
showSuccessMessage();
return;
}
createQuestion(actualQuestion)
}, 1000);
}
// Tela final
function showSuccessMessage() {
hideOrShowQuizz();
// calc score
const score = ((points / questions.length) * 100).toFixed(2);
const scoreDisplay = document.querySelector("#display-score span");
scoreDisplay.textContent = score.toString();
// alterar número de perguntas corretas
const correctAnswers = document.querySelector("#correct-answers");
correctAnswers.textContent = points;
// alterar total de perguntas
const totalQuestions = document.querySelector("#questions-qty");
totalQuestions.textContent = questions.length;
}
// Reiniciar Quizz
const restartBtn = document.querySelector("#restart");
restartBtn.addEventListener("click", function() {
actualQuestion = 0;
points = 0;
hideOrShowQuizz();
init();
});
// Mostra ou exibe o quizz
function hideOrShowQuizz() {
quizzContainer.classList.toggle("hide");
scoreContainer.classList.toggle("hide");
}
// Inicialização
init();