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 trialIvan Kozhunkov
Courses Plus Student 8,480 PointsProblems in my code....
Building a program with two dimensional arrays....
My code:
// Answers
var questions = [
['Which corporation is an owner of Bentley', 'Volkswagen'],
['Which city is a capital of Japan', 'Tokio'],
['The Chevrolet\'s SUV and the lake in California', 'Tahoe'],
];
// variable for an answer
var answer;
// arrays for correctly and incorrectly results
var goodResult = [];
var badResult = [];
function print (message)
{
document.write(message);
}
// choose only first elements of array questions
for ( var i = 0; i < questions.length [i] [0]; i += 1) {
answer += prompt ([i][0]);
}
Something error in the loop Uncaught TypeError: Cannot read property '0' of undefined
What I'm doing incorrectly?
Moderator edited: Added markdown so that the code will display properly on the forums.
1 Answer
Steven Parker
231,269 PointsYou're attempting to index a scalar value.
In your expression "questions.length [i] [0]
", the questions.length part by itself represents a numeric value (the number of questions). Then when you index it using "[i]
", it will return undefined. Then trying to index that using "[0]
" produces the error you see.
What you probably meant to write may have just been "questions.length
".
Also, on the last line where you have "prompt([i][0])
", you probably meant to write "prompt(questions[i][0])
".
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherHi Ivan! I added some markdown to your question so that the code will display properly and be more readable by other students. Check out the Markdown Cheatsheet link at the bottom of the "Add an Answer" section for tips on how to format your code/questions/answers!