feat: implement quiz game functionality with question loading and answer checking
This commit is contained in:
@@ -1 +1,80 @@
|
|||||||
// Agregar aquí el código 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();
|
||||||
@@ -8,7 +8,12 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Ejercicio 9</h1>
|
<h1>Ejercicio 9</h1>
|
||||||
|
<div id="quizGame">
|
||||||
|
<p id="instructions">Responde a la pregunta correcta</p>
|
||||||
|
<p id="question"></p>
|
||||||
|
<ul id="options"></ul>
|
||||||
|
<p id="result"></p>
|
||||||
|
</div>
|
||||||
<script src="ejercicio9.js"></script>
|
<script src="ejercicio9.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user