Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Uncaught TypeError: Cannot read property '0' of undefined: Same Code as Dave, but getting an error.

My code is the same as Dave's in the video except for my questions, but I'm getting this error that makes no sense. I saw a thread on it and the explanation doesn't match my issue.

var questions = [
  ["How many U.S. Presidents currently hold office?", 1]
  ["How many wings does a bird have?", 2],
  ["How many pilots are in a plane?", 2],
  ["How many legs does a bear have?", 2],
];

var correctAnswers = 0;
var question;
var answers;
var response;
var html;

function print(message) {
  document.write(message);
}

for ( var i = 0; i < questions.length; i += 1) {
  question = questions[i][0];
  answer = questions[i][1];
  response = parseInt(prompt(question));
}

2 Answers

0yzh 󠀠
0yzh 󠀠
17,276 Points

Hey, It looks like you are missing a comma after the first array(index 0) of the "questions" array. Also don't forget to add the "if" statement to check if response matches answer so your "correctAnswers" counter goes up if the user inputs the correct answer.

var questions = [
  ["How many U.S. Presidents currently hold office?", 1], // <--- added a comma
  ["How many wings does a bird have?", 2],
  ["How many pilots are in a plane?", 2],
  ["How many legs does a bear have?", 2]
];
for ( var i = 0; i < questions.length; i += 1) {
  question = questions[i][0];
  answer = questions[i][1];
  response = parseInt(prompt(question));
  if (response === answer) {
    correctAnswers++;
  }
}

A comma. DEAR GOD. Thank you. I feel dumb now.