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

179
Views
Why are the answer options for all questions appearing in this single question?

This is a quiz in which each question has 4 answer options. I'm looping through the question one by one and trying to make it appear with its corresponding answer options, but the question is appearing with the answer options of other questions as well. What could I be missing here?

var questionOne = function () {
  // create container to hold questions and answer options & give it a class name
  var questionsDiv = document.createElement("div");
  questionsDiv.className = "questions-div";

  // create question stem, give it a class name, add its content, & append to questionsDiv
  var questionStem = document.createElement("h2");
  questionStem.className = "question-stem";
  questionStem.innerText = quizQuestions[currentQuestion].question;
  questionsDiv.append(questionStem);

  // create container to hold answer option buttons
  var answerOptionsDiv = document.createElement("div");

  // create footer container, give it a class name, & append to the questionsContainer
  var footerDiv = document.createElement("div");
  footerDiv.className = "footer-div";
  questionsContainer.append(footerDiv);

  // create footer text (correct/wrong), give it a class name, & append to footerDiv
  var footerTextCorrect = document.createElement("h2");
  footerTextCorrect.className = "footer-text-correct";
  footerTextCorrect.innerText = "Correct!";
  footerDiv.append(footerTextCorrect);

  var footerTextWrong = document.createElement("h2");
  footerTextWrong.className = "footer-text-wrong";
  footerTextWrong.innerText = "Wrong!";
  footerDiv.append(footerTextWrong);

  // append questionsDiv to main questions container
  questionsContainer.append(questionsDiv);

  // append answerOptionsDiv to questions container
  questionsDiv.append(answerOptionsDiv);

  // loop through the questions
  for (let i = 0; i < quizQuestions.length; i++) {
    console.log(quizQuestions[currentQuestion].question);

    for (let j = 1; j <= 4; j++) {
      console.log(`${j}. ${quizQuestions[currentQuestion].options[`${j}`]}`);

      // create buttons for answer options and add their content
      var optionBtnOne = document.createElement("button");
      optionBtnOne.innerText = `${j}. ${
        quizQuestions[currentQuestion].options[`${j}`]
      }`;
      // append button to answerOptionsDiv
      answerOptionsDiv.append(optionBtnOne);
      optionBtnOne.addEventListener("click", selectAnswer);
      optionBtnOne.setAttribute(
        "id",
        `${quizQuestions[currentQuestion].options[`${j}`]}`
      );
    }
    currentQuestion++;
  }
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You shouldn't have the loop that iterates through the quizQuestions array. This function should just process quizQuestions[currentQuestion]. The currentQuestion index should be incremented when the user goes to the next question, not in this function.

var questionOne = function() {
  // create container to hold questions and answer options & give it a class name
  var questionsDiv = document.createElement("div");
  questionsDiv.className = "questions-div";

  // create question stem, give it a class name, add its content, & append to questionsDiv
  var questionStem = document.createElement("h2");
  questionStem.className = "question-stem";
  questionStem.innerText = quizQuestions[currentQuestion].question;
  questionsDiv.append(questionStem);

  // create container to hold answer option buttons
  var answerOptionsDiv = document.createElement("div");

  // create footer container, give it a class name, & append to the questionsContainer
  var footerDiv = document.createElement("div");
  footerDiv.className = "footer-div";
  questionsContainer.append(footerDiv);

  // create footer text (correct/wrong), give it a class name, & append to footerDiv
  var footerTextCorrect = document.createElement("h2");
  footerTextCorrect.className = "footer-text-correct";
  footerTextCorrect.innerText = "Correct!";
  footerDiv.append(footerTextCorrect);

  var footerTextWrong = document.createElement("h2");
  footerTextWrong.className = "footer-text-wrong";
  footerTextWrong.innerText = "Wrong!";
  footerDiv.append(footerTextWrong);

  // append questionsDiv to main questions container
  questionsContainer.append(questionsDiv);

  // append answerOptionsDiv to questions container
  questionsDiv.append(answerOptionsDiv);
  console.log(quizQuestions[currentQuestion].question);

  for (let j = 1; j <= 4; j++) {
    console.log(`${j}. ${quizQuestions[currentQuestion].options[`${j}`]}`);

    // create buttons for answer options and add their content
    var optionBtnOne = document.createElement("button");
    optionBtnOne.innerText = `${j}. ${
        quizQuestions[currentQuestion].options[`${j}`]
      }`;
    // append button to answerOptionsDiv
    answerOptionsDiv.append(optionBtnOne);
    optionBtnOne.addEventListener("click", selectAnswer);
    optionBtnOne.setAttribute(
      "id",
      `${quizQuestions[currentQuestion].options[`${j}`]}`
    );
  }
};

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!