-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrivia.js
More file actions
57 lines (48 loc) · 1.52 KB
/
Copy pathTrivia.js
File metadata and controls
57 lines (48 loc) · 1.52 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
async function cargarTrivia() {
try {
const response = await fetch("Trivia.json");
const data = await response.json();
const preguntas = data.triviaGeneral;
const seleccionadas = preguntas
.sort(() => 0.5 - Math.random())
.slice(0, 5);
mostrarTrivia(seleccionadas);
} catch (error) {
console.error("Error cargando trivia:", error);
}
}
function mostrarTrivia(preguntas) {
const contenedor = document.getElementById("trivia-container");
const retakeBtn = document.getElementById("retake-btn");
contenedor.innerHTML = "";
retakeBtn.style.display = "none";
preguntas.forEach((p, index) => {
const preguntaDiv = document.createElement("div");
preguntaDiv.classList.add("pregunta");
preguntaDiv.innerHTML = `
<p><b>${index + 1}. ${p.pregunta}</b></p>
${p.opciones
.map(
(opcion) => `
<button class="opcion" onclick="verificarRespuesta(this, '${p.respuesta}')">
${opcion}
</button>
`
)
.join("")}
`;
contenedor.appendChild(preguntaDiv);
});
retakeBtn.style.display = "block";
}
function verificarRespuesta(boton, respuestaCorrecta) {
if (boton.innerText === respuestaCorrecta) {
boton.style.backgroundColor = "green";
} else {
boton.style.backgroundColor = "red";
}
const botones = boton.parentNode.querySelectorAll("button");
botones.forEach((b) => (b.disabled = true));
}
document.getElementById("retake-btn").addEventListener("click", cargarTrivia);
cargarTrivia();