Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

377
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda