-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (67 loc) · 2.76 KB
/
Copy pathscript.js
File metadata and controls
75 lines (67 loc) · 2.76 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
const perguntas = [
{
pergunta: "Qual a capital do Brasil?",
respostas: [
{ texto: "Rio de Janeiro", correta: false },
{ texto: "Brasília", correta: true },
{ texto: "São Paulo", correta: false },
{ texto: "Salvador", correta: false }
]
},
{
pergunta: "Qual o maior planeta do Sistema Solar?",
respostas: [
{ texto: "Terra", correta: false },
{ texto: "Júpiter", correta: true },
{ texto: "Marte", correta: false },
{ texto: "Saturno", correta: false }
]
},
// Adicione mais perguntas aqui!
];
let perguntaAtual = 0;
let pontuacao = 0;
const perguntaElemento = document.getElementById("pergunta");
const respostasElemento = document.getElementById("respostas");
const proximoBtn = document.getElementById("proximo-btn");
const resultadoElemento = document.getElementById("resultado");
function carregarPergunta() {
const pergunta = perguntas[perguntaAtual];
perguntaElemento.textContent = pergunta.pergunta;
respostasElemento.innerHTML = "";
pergunta.respostas.forEach(resposta => {
const li = document.createElement("li");
li.textContent = resposta.texto;
li.addEventListener("click", () => verificarResposta(resposta.correta));
respostasElemento.appendChild(li);
});
}
function verificarResposta(correta) {
if (correta) {
pontuacao++;
respostasElemento.querySelector("li:nth-child(" + (perguntas[perguntaAtual].respostas.findIndex(r => r.correta) + 1) + ")").classList.add("correta");
} else {
respostasElemento.querySelector("li:nth-child(" + (perguntas[perguntaAtual].respostas.findIndex(r => r.correta) + 1) + ")").classList.add("correta");
respostasElemento.querySelector("li:nth-child(" + (perguntas[perguntaAtual].respostas.findIndex(r => !r.correta && respostasElemento.querySelectorAll("li")[perguntas[perguntaAtual].respostas.findIndex(r => !r.correta)].textContent == respostasElemento.querySelectorAll("li")[perguntas[perguntaAtual].respostas.findIndex(r => !r.correta)].textContent) + 1) + ")").classList.add("incorreta");
}
proximoBtn.style.display = "block";
respostasElemento.querySelectorAll("li").forEach(li => li.style.pointerEvents = "none");
}
function proximaPergunta() {
perguntaAtual++;
if (perguntaAtual < perguntas.length) {
carregarPergunta();
proximoBtn.style.display = "none";
respostasElemento.querySelectorAll("li").forEach(li => li.style.pointerEvents = "auto");
} else {
exibirResultado();
}
}
function exibirResultado() {
perguntaElemento.style.display = "none";
respostasElemento.style.display = "none";
proximoBtn.style.display = "none";
resultadoElemento.textContent = `Você acertou ${pontuacao} de ${perguntas.length} perguntas!`;
}
carregarPergunta();
proximoBtn.addEventListener("click", proximaPergunta);