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

375
Views
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 answers
Answer question

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 Report

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 Report

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 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!