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

Chris Reich
15,163 PointsProgram stopping after for loop.
Here is my code: For some reason nothing is printed to the page. However, if I put the print('You got ' + numQuestionsCorrect + 'questions right.'); inside the for loop it prints out three times. I just want it printed once at the end. How can I do this?
var question_Answer = [
['What is the capital of CA?', 'Sacramento'],
['How many planets in our solar system','7'],
['What position did MJ play?','Forward']
];
var numQuestionsCorrect = 0;
var numQuestionsWrong = 0;
for (i = 0; i <= question_Answer.length; i+=1) {
guess = prompt(question_Answer[i][0]);
if (guess === question_Answer[i][1]) {
numQuestionsCorrect+=1;
} else {
numQuestionsWrong+=1;
}
}
print('You got ' + numQuestionsCorrect + 'questions right.');
function print(message) {
document.write(message);
}
1 Answer
Gina Scalpone
21,330 PointsThere's an error in your for loop:
i <= question_Answer.length
There shouldn't be an equals sign in your loop condition because that means i will go to 3. Because arrays are zero-based, even though question_Answer has a length of 3, the indexes will be 0, 1, 2. Basically your loop is looking for question_Answer[3][0] when it doesn't exist. Remove the equals sign and it will work.
for (i = 0; i < question_Answer.length; i+=1)
Jacob Mishkin
23,118 PointsJacob Mishkin
23,118 Pointsedited for formatting.