From 4396167bf16dfccc3807b2dab7cf9911cc0664eb Mon Sep 17 00:00:00 2001 From: Liliana Date: Tue, 12 Mar 2024 19:50:08 +0100 Subject: [PATCH] change check answers --- webapp/src/components/FirstGame.js | 82 ++---------------------------- 1 file changed, 4 insertions(+), 78 deletions(-) diff --git a/webapp/src/components/FirstGame.js b/webapp/src/components/FirstGame.js index f1c88e8a..003a5be6 100644 --- a/webapp/src/components/FirstGame.js +++ b/webapp/src/components/FirstGame.js @@ -8,98 +8,26 @@ import { json } from 'react-router-dom'; import { useLocation } from 'react-router-dom'; -var questionsCalled = false - const Quiz = () => { - const questions = useLocation().state.questions; - console.log(questions) - - // useEffect (() => { - // if (!isApiCalledRef) { - // getQuestions(); - // isApiCalledRef = true; - // } - // }, []); // Dependencia vacía para ejecutar solo al montar el componente - // const getQuestions = async () => { - // try { - // const response = await axios.get(`${apiEndpoint}/questions`); - // for (var i = 0; i < response.data.length; i++) { - // var possibleAnswers = [response.data[i].respuesta_correcta, response.data[i].respuestas_incorrectas[0], response.data[i].respuestas_incorrectas[1], response.data[i].respuestas_incorrectas[2]] - // possibleAnswers = shuffleArray(possibleAnswers) - // console.log(possibleAnswers) - // questions.push({ - // question: response.data[0].pregunta, - // options: possibleAnswers, - // correctAnswer: response.data[i].respuesta_correcta - // }) - // } - // console.log(questions) - - // } catch (error) { - // console.error(error); - // } - // }; - // const getQuestions = async() => { - // try { - // jsonApi = await axios.get(`${apiEndpoint}/questions`); - - // } catch (error) { - // console.log(error.jsonApi.data.error); - // } - // }; - - const [currentQuestionIndex, setCurrentQuestionIndex] = useState(0); - const [selectedOption, setSelectedOption] = useState(null); - const [isCorrect, setIsCorrect] = useState(null); + const [currentQuestionIndex, setCurrentQuestionIndex] = React.useState(0); + const [selectedOption, setSelectedOption] = React.useState(null); + const [isCorrect, setIsCorrect] = React.useState(null); const esperar = (ms) => { return new Promise(resolve => setTimeout(resolve, ms)); }; - function CircularProgress(valorInicial) { - const [percentage, setPercentage] = useState(valorInicial); - - useEffect(() => { - const intervalId = setInterval(() => { - if (percentage <= 0) { - clearInterval(intervalId); // Detener el intervalo - return 0; // Mantener percentage en 0 - } else { - setPercentage(prevPercentage => prevPercentage - 1); // Actualizar el estado con el nuevo porcentaje - } - }, 100); // Intervalo de 100 milisegundos (0.1 segundo) - - // Limpiar el intervalo cuando el componente se desmonte - return () => clearInterval(intervalId); - }, []); // La dependencia vacía asegura que useEffect solo se ejecute una vez al montar el componente - - var listaDevolver = [, percentage] - return ( - listaDevolver - ); - } - - // const [MiCircularProgressbar, MiPercentage] = CircularProgress(100); - - // useEffect(() => { - // if (MiPercentage === 0) { - // // Realizar alguna acción cuando MiPercentage llegue a 0 - // } - // }, [MiPercentage]); // Este efecto se ejecuta cada vez que 'MiPercentage' cambia - - const checkAnswer = async (option) => { setIsCorrect(option === questions[currentQuestionIndex].correctAnswer); setSelectedOption(option); const botonIncorrecta = document.getElementById('option-' + questions[currentQuestionIndex].options.indexOf(option)) if (!isCorrect) { - botonIncorrecta.style.backgroundColor = 'red' - } + const numberAnswer = questions[currentQuestionIndex].options.indexOf(questions[currentQuestionIndex].correctAnswer) const botonCorrecta = document.getElementById('option-' + numberAnswer) botonCorrecta.style.backgroundColor = 'green' @@ -109,10 +37,8 @@ const Quiz = () => { botonIncorrecta.style.backgroundColor = 'lightgrey' botonCorrecta.style.backgroundColor = 'lightgrey' if (questions.length-1 !== currentQuestionIndex) { - setCurrentQuestionIndex((prevIndex) => prevIndex + 1); } - };