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 trialluther wardle
Full Stack JavaScript Techdegree Student 18,029 PointsBuild 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
231,269 PointsI 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]
];
luther wardle
Full Stack JavaScript Techdegree Student 18,029 Pointsthanks! yes my main problem was forgetting to add the "," between my inner list of Arrays haha it runs great now!