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
CodePudding user response:
Make your questions as an array of objects and iterate over it's simple as that here:
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
}]
;
CodePudding user response:
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
}
];
CodePudding user response:
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?
CodePudding user response:
I guess, first of all you could iterate over an array of questions and only then check your answers. But, to my mind, in your repo structure of questions is a bit ambiguous. It's not clear is answers contains set of answers for the quiz (if so why you check this list for correct answer? I mean, this set should contains correct answer anyway, I suppose) or indices of user answers (if so you should check value of index in correctAnswer property against values in answers array).
for (const question of questions) {
if ([check your answers with question.answers and question.correctAnswer]) {
[output: something like question.answers[question.correctAnswer]]
}
}