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

200
Visualizações
Question index not updating when I click next button

I have written this code its just that when I click the next button the questions and answers don't update and go to the next question in the array, but it shows the first question and answers when the page loads. I'm not sure what I havent done here.

const questiontext= document.getElementById('question-text');
const A= document.getElementById('OptionA');
const B= document.getElementById('OptionB');
const C= document.getElementById('OptionC');
const D= document.getElementById('OptionD');
const options= document.getElementsByClassName('options');
const nextbutton= document.getElementById('next');
const submitbutton= document.getElementById('submit');
const questions=[
    {
        question: "What is the best item at mcdonalds?",
        answerA: "Fries",
        answerB: "Big Mac",
        answerC: "Mcnuggets",
        answerD: "Quarter Pounder",
        correctanswer: "Big Mac"
    },
    {
        question: "What is the cheapest thing on the mcdonalds menu?",
        answerA: "Fries",
        answerB: "Double Cheeseburger",
        answerC: "Happy Meal",
        answerD: "Orange juice",
        correctanswer: "Fries"
    },
    {
        question: "What is the least popular item at mcdonalds?",
        answerA: "Filet O Fish",
        answerB: "Hamburger",
        answerC: "Veggie Deluxe",
        answerD: "Mineral water",
        correctanswer: "Filet O Fish"
    },
    {
        question: "How many dips are you allowed with 20 Mcnuggets?",
        answerA: "2",
        answerB: "4",
        answerC: "3",
        answerD: "6",
        correctanswer: "4"
    }
];
//Question index at start
const questionindex= 0;
const currentquestion= () =>{
    questiontext.innerHTML= questions[questionindex].question;
    A.innerHTML= questions[questionindex].answerA;
    B.innerHTML= questions[questionindex].answerB;
    C.innerHTML= questions[questionindex].answerC;
    D.innerHTML= questions[questionindex].answerD;
    if(questionindex === questions.length){
       submitbutton.classList.remove('hidden'); 
    }
}

const nextquestion= () =>{
    questionindex++
}
//Load first question and answers
currentquestion(questionindex);
//Button to display next question
nextbutton.addEventListener('click', nextquestion);
   

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

0

Change

const questionindex 

to

let questionindex
about 4 years ago · Juan Pablo Isaza Relatório

0

change the const and replace it with a let or var

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