Glad you're here to check this out. See the code below.
let questions = [];
let Question = function(question, answers, number) {
this.question = question;
this.answers = answers;
this.number = number;
}
let question1 = new Question('Is JavaScript the best programming language?',
['No', 'Yes'], 1);
let question2 = new Question('Is JavaScript a web development language?',
['No', 'Yes'], 1);
let question3 = new Question(`What is JavaScript's official server side
application called?`, ['BootStrap', 'Python', 'Node.js'], 2);
questions = [question1, question2, question3];
Question.randomQuestion = function() {
let randomNum = Math.floor(Math.random() * 3);
let randomQuestion = questions[randomNum];
return randomQuestion;
}
Question.display = function(randomQuestion) {
console.log(randomQuestion.question);
debugger;
// Why does the conditional give me undefined?
for (let i = 0; i < questions[i].answers[i].length; i++)
console.log(`${randomQuestion.answers[i]}`);
}
I'm trying to find the answers property length in each iteration, but I keep getting an error that says answers is undefined. Any idea why this is happening and a better way to achieve this?