Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
Cómo acceder al siguiente objeto en una matriz incrementando

 let myQuestions = [ { question: "What's 2+2?", answers: [ { text: "4", correct: true }, { text: "2", correct: false }, { text: "10", correct: false }, { text: "1", correct: false }, ], }, { question: "What's 10+10?", answers: [ { text: "20", correct: true }, { text: "2", correct: false }, { text: "18", correct: false }, { text: "0", correct: false }, ], }, ]; function startQuiz() { //container.style.visibility = "visible"; //btn_start.style.visibility = "hidden"; showQuestion(myQuestions[0]); } function showQuestion(questionAndAnswers) { alert(questionAndAnswers.question); //const shuffledAnswers = _.shuffle(questionAndAnswers.answers); //questionTag.innerText = questionAndAnswers.question; //answerTag[0].innerText = shuffledAnswers[0].text; //answerTag[1].innerText = shuffledAnswers[1].text; //answerTag[2].innerText = shuffledAnswers[2].text; //answerTag[3].innerText = shuffledAnswers[3].text; } function nextQuestion() { showQuestion(myQuestions[0]); }
 <input type='button' value='Start Quiz' onclick=startQuiz() /> <input type='button' value='Next question' onclick=nextQuestion() />

La primera pregunta se llama cuando presiono iniciar cuestionario y accede a mi primer objeto en mi matriz, ¿cómo hago para que funcione la función siguiente pregunta que básicamente accede al siguiente objeto incrementando, así que digamos que agrego la pregunta 3 y continúa?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Tome una variable global let currentQuestionIndex = 0; y actualice su función nextQuestion como se muestra a continuación. También actualice su función startQuiz() y establezca currentQuestionIndex = 0; & replace showQuestion(myQuestions[0]); con nextQuestion();

 function nextQuestion() { // Add condition so at last question it will stop at last question if (myQuestions.length > currentQuestionIndex) { showQuestion(myQuestions[currentQuestionIndex]); currentQuestionIndex++; } } function startQuiz() { container.style.visibility = "visible"; btn_start.style.visibility = "hidden"; currentQuestionIndex = 0; nextQuestion(); }

Su código completo se verá a continuación. He comentado parte de su código solo con fines de prueba.

 let currentQuestionIndex = 0; let myQuestions = [ { question: "What's 2+2?", answers: [ { text: "4", correct: true }, { text: "2", correct: false }, { text: "10", correct: false }, { text: "1", correct: false }, ], }, { question: "What's 10+10?", answers: [ { text: "20", correct: true }, { text: "2", correct: false }, { text: "18", correct: false }, { text: "0", correct: false }, ], }, ]; function startQuiz() { //container.style.visibility = "visible"; //btn_start.style.visibility = "hidden"; currentQuestionIndex = 0; nextQuestion(); } function showQuestion(questionAndAnswers) { // temp code for alert alert(questionAndAnswers.question); //const shuffledAnswers = _.shuffle(questionAndAnswers.answers); //questionTag.innerText = questionAndAnswers.question; //answerTag[0].innerText = shuffledAnswers[0].text; //answerTag[1].innerText = shuffledAnswers[1].text; //answerTag[2].innerText = shuffledAnswers[2].text; //answerTag[3].innerText = shuffledAnswers[3].text; } function nextQuestion() { // Add condition so at last question it will stop at last question if (myQuestions.length > currentQuestionIndex) { showQuestion(myQuestions[currentQuestionIndex]); currentQuestionIndex++; } }
 <input type='button' value='Start Quiz' onclick=startQuiz() /> <input type='button' value='Next question' onclick=nextQuestion() />

about 4 years ago · Juan Pablo Isaza Relatório

0

Puede almacenar su índice de preguntas actual y usarlo para recorrer la lista:

 let currentQuestionIndex = 0; let myQuestions = [ { question: "What's 2+2?", answers: [ { text: "4", correct: true }, { text: "2", correct: false }, { text: "10", correct: false }, { text: "1", correct: false }, ], }, { question: "What's 10+10?", answers: [ { text: "20", correct: true }, { text: "2", correct: false }, { text: "18", correct: false }, { text: "0", correct: false }, ], }, ]; function startQuiz() { container.style.visibility = "visible"; btn_start.style.visibility = "hidden"; showQuestion(myQuestions[0]); } function showQuestion(questionAndAnswers) { const shuffledAnswers = _.shuffle(questionAndAnswers.answers); questionTag.innerText = questionAndAnswers.question; answerTag[0].innerText = shuffledAnswers[0].text; answerTag[1].innerText = shuffledAnswers[1].text; answerTag[2].innerText = shuffledAnswers[2].text; answerTag[3].innerText = shuffledAnswers[3].text; } function nextQuestion() { const nextIndex = currentQuestionIndex + 1; if (nextIndex < myQuestions.length - 1) { showQuestion(myQuestions[nextIndex]); currentQuestionIndex = nextIndex; } else { console.log('end of question list'); } }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda