Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
¿Cómo genero una pregunta aleatoria usando javascript para mi aplicación de prueba que no repite la misma pregunta?

Estoy tratando de generar preguntas aleatorias a partir de mi objeto de "preguntas" que no se repite a sí mismo, pero mi aplicación de prueba está repitiendo preguntas.

Por favor, perdóname si mi pregunta no es lo suficientemente clara. Esta es la primera vez que hago una pregunta o realmente uso stack overfolow,

 let selectedQuestions = []; //creates random index between 0 and the length of any array passed into it const getRandomIndex = (arr) => Math.floor(Math.random() * arr.length); //uses getRandomIndex to generate select a random object from and array. Also checks to see if the random object has been selected, if not, pushes the question to the empty selectedQuestions array and if selected, loops through its own function with a recursion by calling itself until it finds a question that has not already been pushed to the selectedQuestions array. const getRandomObject = (arr) => { const random = getRandomIndex(arr); if (selectedQuestions.includes(random)) { getRandomObject(arr); } selectedQuestions.push(random); console.log("selected questions:", selectedQuestions.length) return arr[random]; //renders the selected array and questions in the correct section of the quiz board. const randomGenerator = () => { const data = getRandomObject(questions); progress.textContent = `Question ${gameState.questionCounter} of ${maxQuestion}` questionBoard.textContent = `Q${gameState.questionCounter}. ${data.question}`; currentCorrectAnswer = data.answer; userAnswer.forEach((answer, index) => { answer.innerHTML = `${alphabet[index]}. ${data.choices[index]}`; }); currentUserAnswer = null; clearAll() gameState.questionCounter++ nextBtn.disabled = true; setDisabledStateForQuizAnswers(false); } }

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Te falta una declaración de return en la función getRandomObject .

 if (selectedQuestions.includes(random)) { return getRandomObject(arr); }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!