-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScripts.js
192 lines (164 loc) · 4.34 KB
/
Scripts.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
const question = document.querySelector("#question");
const answersBox = document.querySelector("#answers-box");
const quizzContainer = document.querySelector("#quizz-container");
const scoreContainer = document.querySelector("#score-container");
const alternativas = ["a", "b", "c", "d"];
let pontuacao = 0;
let perguntaAtual = 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,
},
],
},
];
// Função que inicia o jogo
function init() {
createQuestion(0);
}
function createQuestion(i) {
// i = 0
// limpar a questão anterior
const oldButtons = answersBox.querySelectorAll("button");
oldButtons.forEach(function (btn) {
btn.remove();
});
// Alterar o texto da pergunta
const questionText = question.querySelector("#question-txt");
const questionNumber = question.querySelector("#question-number");
questionText.innerText = questions[i].question;
questionNumber.innerText = i + 1;
// Criar o template do botão do quizz
questions[i].answers.forEach(function (answer, i) {
const answerTemplate = document
.querySelector(".answer-template")
.cloneNode(true);
const letterBtn = answerTemplate.querySelector(".btn-letter");
const answerTxt = answerTemplate.querySelector(".question-answer");
letterBtn.innerText = alternativas[i];
answerTxt.innerText = answer.answer;
answerTemplate.setAttribute("correct-answer", answer.correct);
// Remover o hide e answer template class
answerTemplate.classList.remove("hide");
answerTemplate.classList.remove("answer-template");
// inserindo a pergunta no answersbox
answersBox.appendChild(answerTemplate);
// Inserindo evento de click no botão
answerTemplate.addEventListener("click", function () {
checkAnswer(answerTemplate);
});
});
// Incrementando a pergunta atual
perguntaAtual++;
}
// Verificando a resposta do usuário
function checkAnswer(btn) {
const buttons = answersBox.querySelectorAll("button");
buttons.forEach(function (button) {
if (button.getAttribute("correct-answer") == "true") {
button.classList.add("correct-answer");
// Checa se o usuário acertou a perguntou
if (btn == button) {
pontuacao++;
}
} else {
button.classList.add("wrong-answer");
}
});
nextQuestion();
}
function nextQuestion() {
setTimeout(function () {
// Verifica se ainda há pergunta na lista
if (perguntaAtual >= questions.length) {
// apresenta a mensagem de sucesso
showResult();
return;
}
createQuestion(perguntaAtual);
}, 700);
}
function showResult() {
hideOrShowQuizz();
// calcular a pontuação
const score = ((pontuacao / questions.length) * 100).toFixed(2);
const displayScore = document.querySelector("#display-score");
displayScore.innerText = score + "%";
// Alterar o número de respostas corretas
const correctAnswers = document.querySelector("#correct-answers");
correctAnswers.innerText = pontuacao;
const qtd = document.querySelector("#questions-qtd");
qtd.innerText = questions.length;
}
function hideOrShowQuizz() {
quizzContainer.classList.toggle("hide");
scoreContainer.classList.toggle("hide");
}
// Reiniciar o quizz
const restart = document.querySelector("#restart");
restart.addEventListener("click", function () {
pontuacao = 0;
perguntaAtual = 0;
hideOrShowQuizz();
init();
});
init();