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 Arrays Multidimensional Arrays Build a Quiz Challenge – One Solution

Why I get result of "Uncaught TypeError: Cannot read property '0' of undefined"

let QandA = [["What's is my name?", "AMY"], ["Who is my boyfriend?", "KEVIN"], ["What is my favorite sport?", "BASKETBALL"], ["Where do I live?", "TAIPEI"], ["Who is my favorite cat?", "BON"]];

let correctAnswer = 0;

for (let i=0; i<=QandA.length; i++) {

if (prompt(QandA[i][0]).toUpperCase() === QandA[i][1]) { correctAnswer += 1; } } let messeage = You got ${correctAnswer} correct out of 5 questions.; document.querySelector('main').innerHTML = messeage ;

Jesse Ward
Jesse Ward
14,880 Points

Sorry, meant to answer, not comment

1 Answer

Jesse Ward
Jesse Ward
14,880 Points

Check your for loop. you're testing while 'i' is less than or equal to the length of the array and then increasing the value of 'i'. When the loop gets to the length of the array, it continues to increase the length of 'i' one more, to actually be longer than the array. Your for loop should read:

for (let i=0; i<QandA.length; i++) {}