From 0e64f4ecf436a49d3b0ebf91b04f379ba55b5686 Mon Sep 17 00:00:00 2001 From: German De los Santos Date: Sat, 11 Nov 2023 21:10:35 -0300 Subject: [PATCH] se le agrego mas data al archivo Json para que sea mas largo el juego --- js/preguntasJson.json | 27 +++++++++++++++- js/sript.js | 75 ++++++++++++++++++++++--------------------- 2 files changed, 65 insertions(+), 37 deletions(-) diff --git a/js/preguntasJson.json b/js/preguntasJson.json index b9c5ddf..49c3760 100644 --- a/js/preguntasJson.json +++ b/js/preguntasJson.json @@ -13,5 +13,30 @@ "question": "¿Cuál es el Pokémon legendario que representa el tiempo?", "option": ["a) Dialga", "b) Palkia", "c) Giratina"], "correctAnswer": 0 + }, + { + "question": "¿Cuál es el Pokémon que puede megaevolucionar en las dos versiones X e Y, pero con megaevoluciones diferentes en cada una?", + "option": ["a) Charizard", "b) Mewtwo", "c) Lucario"], + "correctAnswer": 1 + }, + { + "question": "¿Cuál es el Pokémon que tiene la habilidad 'Levitón' y es inmune a todos los ataques de tipo Tierra?", + "option": ["a) Gengar", "b) Flygon", "c) Magnezone"], + "correctAnswer": 1 + }, + { + "question": "¿Cuántos Pokémon hay en total hasta la séptima generación, incluyendo formas alternativas?", + "option": ["a) 721", "b) 802", "c) 893"], + "correctAnswer": 1 + }, + { + "question": "¿Cuál es el Pokémon conocido por cambiar de forma según el clima y la hora del día?", + "option": ["a) Castform", "b) Shaymin", "c) Mimikyu"], + "correctAnswer": 0 + }, + { + "question": "¿Qué Pokémon tiene la habilidad 'Presión' y es conocido por ser el guardián de la Torre Hojalata?", + "option": ["a) Lugia", "b) Ho-Oh", "c) Suicune"], + "correctAnswer": 2 } -] \ No newline at end of file +] diff --git a/js/sript.js b/js/sript.js index 579c13b..0ec3942 100644 --- a/js/sript.js +++ b/js/sript.js @@ -61,6 +61,7 @@ function displaySavedQuestion() { const savedQuestionString = localStorage.getItem('Savedquestions'); const savedQuestion = JSON.parse(savedQuestionString); const savedScore = JSON.parse(localStorage.getItem('savedScore')); + if (savedQuestion != null && savedQuestion.length > 0 ) { questions = savedQuestion; score = savedScore; @@ -71,59 +72,61 @@ function displaySavedQuestion() { }; // funcion pa mostrar las preguntas - function displayCurrentQuestion() { - if (questions.length > 0) { - currentQuestionIndex = (Math.floor(Math.random() * (1 + questions.length - 1))); - const currentQuestion = questions[currentQuestionIndex]; - startTimer(); - questionHtmlElement.innerHTML = ''; - for (let i = 0; i < currentQuestion.question.length; i++) { - const span = document.createElement('span'); - span.setAttribute('class', 'fall letras'); - span.setAttribute('style', `animation-delay: ${i / 20}s`); - span.textContent = currentQuestion.question[i]; - questionHtmlElement.appendChild(span); - console.log(currentQuestion.question); - } - - optionAButton.removeEventListener('click', handleOptionAClick); - optionBButton.removeEventListener('click', handleOptionBClick); - optionCButton.removeEventListener('click', handleOptionCClick); - - - optionAButton.textContent = currentQuestion.option[0]; - optionBButton.textContent = currentQuestion.option[1]; - optionCButton.textContent = currentQuestion.option[2]; - colorButons(); - - // Agregar eventos click para los botones de opción - optionAButton.addEventListener('click',handleOptionAClick); - - optionBButton.addEventListener('click',handleOptionBClick); - - optionCButton.addEventListener('click',handleOptionCClick); +function displayCurrentQuestion() { + if (questions.length > 0) { + currentQuestionIndex = Math.floor(Math.random() * questions.length); + const currentQuestion = questions[currentQuestionIndex]; + + // Verifica si currentQuestion es un objeto válido y tiene las propiedades necesarias + if (currentQuestion && currentQuestion.hasOwnProperty('question') && currentQuestion.hasOwnProperty('option') && currentQuestion.hasOwnProperty('correctAnswer')) { + startTimer(); + questionHtmlElement.innerHTML = ''; + for (let i = 0; i < currentQuestion.question.length; i++) { + const span = document.createElement('span'); + span.setAttribute('class', 'fall letras'); + span.setAttribute('style', `animation-delay: ${i / 20}s`); + span.textContent = currentQuestion.question[i]; + questionHtmlElement.appendChild(span); + console.log(currentQuestion.question); + } + + optionAButton.removeEventListener('click', handleOptionAClick); + optionBButton.removeEventListener('click', handleOptionBClick); + optionCButton.removeEventListener('click', handleOptionCClick); + optionAButton.textContent = currentQuestion.option[0]; + optionBButton.textContent = currentQuestion.option[1]; + optionCButton.textContent = currentQuestion.option[2]; + colorButons(); + + // Agregar eventos click para los botones de opción + optionAButton.addEventListener('click', handleOptionAClick); + optionBButton.addEventListener('click', handleOptionBClick); + optionCButton.addEventListener('click', handleOptionCClick); + } else { + // Si currentQuestion no tiene el formato esperado, mostrar un mensaje de error o realizar alguna acción de manejo. + console.error('Formato de pregunta no válido:', currentQuestion); + } } else { clearInterval(countdownTimer); timer.remove(); - questionHtmlElement.textContent = `Juego terminado ${score > 0 ? 'tu puntaje es : ' + score : 'Perdiste Bobo'}`; + questionHtmlElement.textContent = `Juego terminado ${score > 0 ? 'tu puntaje es : ' + score : 'Perdiste Bobo'}`; optionAButton.style.display = 'none'; optionBButton.style.display = 'none'; optionCButton.style.display = 'none'; const resetButton = document.createElement('button'); resetButton.setAttribute('id', 'resetButton'); - resetButton.textContent = ('Reniciar Juego') + resetButton.textContent = ('Reniciar Juego'); buttonDiv.append(resetButton); - resetButton.addEventListener('click',() => { + resetButton.addEventListener('click', () => { score = 0; localStorage.clear(); window.location.reload(); - }); } -}; +} // Función para iniciar el temporizador function startTimer() {