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

olu adesina
olu adesina
23,007 Points

why is this happening :Uncaught TypeError: Cannot read property '2' of undefined

/var questionsAnswers contains three values. A question and answer and false value which will change when the correct answer is submited///

var questionsAnswers =[ ['What is the capital of England? ','LONDON',false], ['what is the capital of France?','PARIS',false], ['What organ pumps blood around the body?','HEART',false]

];

var ask; var rightAnswers = 0

function print(message) {

document.write(message); }

for(x=0;x<questionsAnswers.length;x+=1){ ask=prompt(questionsAnswers[x][0]); if(ask.toUpperCase()==questionsAnswers[x][1]){ questionsAnswers[x][2]=true /* this is where the false statment changes to true */ rightAnswers+=1;

}

}

print('you got '+rightAnswers+' questions right!<BR><BR>')

print('YOU GOT THESE QUESTIONS CORRECT <br><br>') for(y=0;questionsAnswers.length;y+=1) {

if(questionsAnswers[y][2]) //Uncaught TypeError: Cannot read property '2' of undefine at quiz.js:35 {print(questionsAnswers[y][0]+'<br><br>')} }

/* print('YOU GOT THESE QUESTIONS WRONG <BR>'); for(y=0;questionsAnswers.length;y+=1) {

if(questionsAnswers[y][2]==false) {print(questionsAnswers[y][0]+'<br><br>')} }*/

Steven Parker
Steven Parker
229,708 Points

When posting code, please use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:

2 Answers

Steven Parker
Steven Parker
229,708 Points

You have an incomplete loop condition.

When printing out the questions answered correctly, you have a for loop with this condition: "questionsAnswers.length", but you probably meant to write: "y < questionsAnswers.length". This causes the loop to run too long and exhaust the available data.

You have the same issue in the commented-out code that would show the questions answered wrong.

olu adesina
olu adesina
23,007 Points

sorry about the way in which it was formatted have a bit of trouble doing it right

Ryan Rankin
Ryan Rankin
8,261 Points

This is nearly impossible to read. At the bottom of the input field where you are typing your question there is a link called "Markdown Cheatsheet". Please reference that and redo your question so it is made easier for us to read and help. Good luck!