J Akhtar
J Akhtar

Reputation: 667

undefined on first array element in javascript

I am trying out a simple quiz application in JavaScript. I have array of questions object and when I loop through the array and print out the questions it says undefined on the first one but second one is printing the questions to the console.log. Any knows whats happening here.

Thanks.

var questions = 
    [
      
      {
        questions: "What color is of milk?",
        choices: ["White", "Blue", "Brown", "Red"],
        answer: 0
      },
      
      {
        question: "What is Tallest Building in the world?",
        choices: ["Eifle Tower","Burg Khalifa", "Shenghai Tower" ],
        answer: 1
      }
    ];


  for ( var i = 0; i < questions.length; i++ ) {
     question = questions[i].question;
     choices = questions[i].choices;
     answer = questions[i].answer;
    
    console.log ( question );
    console.log ( choices );
    console.log ( answer );
  
  }

Upvotes: 0

Views: 928

Answers (1)

phreakv6
phreakv6

Reputation: 2165

Shouldn't questions: "What color is of milk?", be question: "What color is of milk?",

This works fine.

var questions = 
    [

      {
        question: "What color is of milk?",
        choices: ["White", "Blue", "Brown", "Red"],
        answer: 0
      },

      {
        question: "What is Tallest Building in the world?",
        choices: ["Eifle Tower","Burg Khalifa", "Shenghai Tower" ],
        answer: 1
      }
    ];


  for ( var i = 0; i < questions.length; i++ ) {
     question = questions[i].question;
     choices = questions[i].choices;
     answer = questions[i].answer;

    console.log ( question );
    console.log ( choices );
    console.log ( answer );

  }

Upvotes: 2

Related Questions