diff --git a/ejercicio9/ejercicio9.js b/ejercicio9/ejercicio9.js index 6ce9e92..f354439 100644 --- a/ejercicio9/ejercicio9.js +++ b/ejercicio9/ejercicio9.js @@ -1 +1,80 @@ // 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(); \ No newline at end of file diff --git a/ejercicio9/index.html b/ejercicio9/index.html index 2d407da..979701a 100644 --- a/ejercicio9/index.html +++ b/ejercicio9/index.html @@ -8,7 +8,12 @@

Ejercicio 9

- +
+

Responde a la pregunta correcta

+

+ +

+