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

Jesse Jones
Jesse Jones
2,639 Points

Hello, please help me understand this error.

Here is the error: Uncaught TypeError: Cannot read property '0' of undefined at quiz.js:15

(Line 15 of my code is the line directly under my for loop with the variable answer).

When I preview the page it is asking me all three questions in the array, but then it stops working. I'm assuming it's not able to store the answers. Something to do with there being three answers perhaps?

Thank you for the help. I hope this all displays properly; this is my first question on Treehouse :)

Here is the code:

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

var questions = [ ["What's 2 times 2?", 4], ["What's 1 times 2?", 2], ["What's 3 times 2?", 6] ];

var correctAnswers = 0;

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

print('You got ' + correctAnswers + ' right, good job!');

1 Answer

Jesse Jones
Jesse Jones
2,639 Points

My apologies, I cannot seem to delete this question. Please disregard. I asked the question with better formatting in another post.