Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

luther wardle
seal-mask
.a{fill-rule:evenodd;}techdegree
luther wardle
Full Stack JavaScript Techdegree Student 18,029 Points

Build a quiz challenge-using 2 dimensional arrays:

Greetings, Here is my code to build a quiz using arrays The console returns this error

Uncaught TypeError: Cannot read property '0' of undefined at quiz.js:12 I checked and for some reason, it can't read my array at [i][0];

can anyone explain my problem? here's my code:

var questions = [ ['how many apples are in a tree','7'] ['how many groundhogs are there','1'] ];

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

for(i=0;i<questions.length;i++){ question = questions[i][0]; answer = questions[i][1]; response=parseInt(prompt(question)); if(response===answer){ correctAnswers+=1; } } html='you got'+correctAnswers+'questions correct'; print(html);

2 Answers

Steven Parker
Steven Parker
210,871 Points

I see two issues with the declaration of "questions":

  • there should be a comma between the two inner arrays
  • based on how the code uses them, the answers should be numbers instead of strings
var questions = [
  ["how many apples are in a tree", 7],
  ["how many groundhogs are there", 1]
];