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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1

Benjamin Cohen
seal-mask
.a{fill-rule:evenodd;}techdegree
Benjamin Cohen
Full Stack JavaScript Techdegree Student 6,045 Points

I continue to get the following error: TypeError: quiz[i] is undefined

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

var quiz = [
  ['Who was the first president?', 'george washington'],
  ['Who was the sixteenth president?', 'abraham lincoln'],
  ['Who is the current president?', 'donald trump']
];
var answer;
var correctAnswer = 0;

for (var i = 0; i <= quiz.length; i += 1) {
  answer = prompt(quiz[i][0]).toLowerCase();
  if (answer === quiz[i][1]) {
    correctAnswer += 1;
    print("You got question " + (i+1) + " right.  Correct Answers: " + correctAnswer + " <br>");
  }
}

print("You got " + correctAnswer + " questions right.")
Benjamin Cohen
seal-mask
.a{fill-rule:evenodd;}techdegree
Benjamin Cohen
Full Stack JavaScript Techdegree Student 6,045 Points

The for loop works when it's iterating through the 2D array, meaning I see this as I answer the questions;

You got question 1 right. Correct Answers: 1 You got question 2 right. Correct Answers: 2 You got question 3 right. Correct Answers: 3

But when I try and print the 'correctAnswer' variable after the loop ends I get an error.

2 Answers

jamesjones21
jamesjones21
9,260 Points

the issue lays within the the conditional part of the for loop, it itterates over the array and then i becomes 3, so in the array there is no key 3 inside of the quiz array. (keys are only 0,1,2) so that is why you are getting undefined. If you remove the equal sign just after the less than, it should then output the correct answers for you and undefined should not show:

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

var quiz = [
  ['Who was the first president?', 'George Washington'],
  ['Who was the sixteenth president?', 'Abraham Lincoln'],
  ['Who is the current president?', 'Donald Trump']
]
var answer;
var correctAnswer = 0;

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

print("You got " + correctAnswer + " questions right.")
jamesjones21
jamesjones21
9,260 Points

I would also try and debug it in the browser debugger, and try and step over it. Then for each question is asked you can hover your cursor over i, So after the last one entry in the array i is 2 then becomes 3 so i is 3 and length of the array is 3 so it would jump into the loop, but then cannot read anything as key 3 doess't exist.

Glad to help