80 lines
2.7 KiB
JavaScript
80 lines
2.7 KiB
JavaScript
// Agregar aquí el código javascript
|
|
|
|
const questions = [
|
|
{
|
|
question: "¿Cuál es la capital de Francia?",
|
|
options: ["Londres", "Berlín", "París", "Roma"],
|
|
correctAnswer: 2
|
|
},
|
|
{
|
|
question: "¿Cuál es el río más largo del mundo?",
|
|
options: ["Nilo", "Amazonas", "Yangtsé", "Misisipi"],
|
|
correctAnswer: 1
|
|
},
|
|
{
|
|
question: "¿Quién escribió 'Cien años de soledad'?",
|
|
options: ["Gabriel García Márquez", "Mario Vargas Llosa", "Isabel Allende", "Jorge Luis Borges"],
|
|
correctAnswer: 0
|
|
},
|
|
{
|
|
question: "¿Cuál es el planeta más grande del sistema solar?",
|
|
options: ["Júpiter", "Saturno", "Urano", "Neptuno"],
|
|
correctAnswer: 0
|
|
},
|
|
{
|
|
question: "¿En qué año se descubrió América?",
|
|
options: ["1492", "1500", "1485", "1510"],
|
|
correctAnswer: 0
|
|
}
|
|
];
|
|
|
|
let currentQuestionIndex = 0;
|
|
let score = 0;
|
|
|
|
const questionElement = document.getElementById('question');
|
|
const optionsElement = document.getElementById('options');
|
|
const resultElement = document.getElementById('result');
|
|
|
|
const loadQuestion = () => {
|
|
const currentQuestion = questions[currentQuestionIndex];
|
|
questionElement.textContent = currentQuestion.question;
|
|
optionsElement.innerHTML = '';
|
|
currentQuestion.options.forEach((option, index) => {
|
|
const li = document.createElement('li');
|
|
const button = document.createElement('button');
|
|
button.textContent = option;
|
|
li.appendChild(button);
|
|
button.addEventListener('click', () => checkAnswer(index));
|
|
optionsElement.appendChild(li);
|
|
});
|
|
};
|
|
|
|
const checkAnswer = (selectedIndex) => {
|
|
const currentQuestion = questions[currentQuestionIndex];
|
|
if (selectedIndex === currentQuestion.correctAnswer) {
|
|
resultElement.textContent = "¡Correcto!";
|
|
score++;
|
|
currentQuestionIndex++;
|
|
if (currentQuestionIndex < questions.length) {
|
|
setTimeout(() => {
|
|
resultElement.textContent = '';
|
|
loadQuestion();
|
|
}, 1000);
|
|
} else {
|
|
resultElement.textContent = `¡Juego terminado! Tu puntuación es ${score}/${questions.length}.`;
|
|
}
|
|
} else {
|
|
resultElement.textContent = "Incorrecto. La respuesta correcta es: " + currentQuestion.options[currentQuestion.correctAnswer];
|
|
currentQuestionIndex++;
|
|
if (currentQuestionIndex < questions.length) {
|
|
setTimeout(() => {
|
|
resultElement.textContent = '';
|
|
loadQuestion();
|
|
}, 2000);
|
|
} else {
|
|
resultElement.textContent = `¡Juego terminado! Tu puntuación es ${score}/${questions.length}.`;
|
|
}
|
|
}
|
|
};
|
|
|
|
loadQuestion(); |