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

382
Visualizações
How do I check answers in array objects by its index?

I am trying to build a quiz in JavaScript. I have an object with a question, answers and the correct answer. I can check whether the user has answered the question correctly by matching the index from the answers to the index of the correct answer.

However, when I try to use multiple objects and try to run this code, it doesn't work. How can I make this work?`

this works:

let questions = {
  question: "How many sides does a square have?",
  answers: [4, 6, 8],
  correctAnswer: 0,
  category: "trueOrFalse"
};

const iterator = questions.answers.keys();
for (const key of iterator) {
  if (key === questions.correctAnswer) {
    console.log(questions.question + ": " + questions.answers[key]);
  }
}

but this doesn't work:

let questions =
  {
    question: "How many sides does a square have?",
    answers: [4, 6, 8],
    correctAnswer: 0
  },
  {
    question: "How many sides does a triangle have?",
    answers: [3, 6, 8],
    correctAnswer: 0
  }
;

const iterator = questions.answers.keys();
for (const key of iterator) {
  if (key === questions.correctAnswer) {
    console.log(questions.question + ": " + questions.answers[key]);
  }
}

Here is the link to my repo: DC Quiz

The problem occurs when I try to answer question 4. In my code you can see it is faulty in the main.js file on row 206.

Thank you for any help! Michiel

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

0

as already stated above, you need to define your questions in an array. Like:

const questions = [{
    question: "How many sides does a triangle have?",
    answer: [3, 5, 7],
    correctAnswer: 0
  },
  {
    question: "some question?",
    answer: [1, 42, "another answer"],
    correctAnswer: 2
  }
]

You are probably making it harder than you have to by using .keys(), this array function returns an iterator with the index numbers instead of the actual values, read about it here

Since you are storing the index to the right answer you can easily solve this by looping through your questions array and deal with the answers like this:

for (let q of questions) {
  console.log(q.question, ": ", answers[correctAnswer])
}

This works since correctAnswer holds the index key for the right answer.

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to define your questions as an array of objects.

let questions = [
  {
    question: "How many sides does a square have?",
    answers: [4, 6, 8],
    correctAnswer: 0
  },
  {
    question: "How many sides does a triangle have?",
    answers: [3, 6, 8],
    correctAnswer: 0
  }
];
about 4 years ago · Juan Pablo Isaza Relatório

0

you have to iterate through your questions array as well. the first question is in:

questions[0].question

Btw: your questions variable should be an array. (like your answers) try the following:

    let questions =
  [{
    question: "How many sides does a square have?",
    answers: [4, 6, 8],
    correctAnswer: 0
  },
  {
    question: "How many sides does a triangle have?",
    answers: [3, 6, 8],
    correctAnswer: 0
  }
;

const iterator = questions.answers.keys();
for (const key of iterator) {
  if (key === questions.correctAnswer) {
    console.log(questions.question + ": " + questions.answers[key]);
  }
}]

Explanation:

The questions array bundles all your question objects. If you want to ask a specific question you need to tell javascript which question object it is that you want to ask.

Is it the first question?:

question[0].question

meaning: "How many sides does a square have?"

or maybe another one?

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