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

Johnny Harrison
Johnny Harrison
4,331 Points

I've tried everything. I can't find the bug

Snapshot to my workspace: http://w.trhou.se/uigt3ey2pc

When I run this program, it will spit out the correct result only if every answer is true(TTT), or every answer is false(FFF). If I answer in any other combination (FTT), (TFF), (TTF) etc, some of the values will print to the screen, and others will come back as undefined.

Here's the loop that runs through every question:

for ( var i = 0; questions[0].length>=i; i++) {
 guesses.push(prompt(questions[i][0]));

  if (guesses[i] === questions[i][1]) {
    correct.push(questions[i][0]);
    correctList += "<li>" + correct[i] + "</li>";
    counter++;
  } else if (guesses[i] !== questions[i][1]) {
    incorrect.push(questions[i][0]);
    incorrectList += "<li>" + incorrect[i] + "</li>";
  }
}

And here are some console tests I ran after answering the quiz (TFF):

>correct
["What is 5 + 8?"]
>correctList
"<ol><li>What is 5 + 8?</li></ol>"
//good, that's working as intended

>incorrect
(2) ["The primary colors are red, yellow and:", "Hot is to red as cold is to:"]
//looking good

>incorrectList
"<ol><li>undefined</li><li>undefined</li></ol>"
//What?!

the problem seems to be with passing them into the 'incorrectList' string.

1 Answer

Kyle Johnson
Kyle Johnson
33,528 Points

The reason your list items are undefined is because the correctList or incorrectList is referencing the wrong position[i] in the correct or incorrect arrays. You don't want to know what the array values are for 0, 1 and 2 if let's say the correct array has 0 and the incorrect array has 0, 1.

You need to remove [i] from

correctList += "<li>" + correct[i] + "</li>";

and

incorrectList += "<li>" + incorrect[i] + "</li>";
Kyle Johnson
Kyle Johnson
33,528 Points

You could actually refactor your code like this too:

var questions = [
  ["What is 5 + 8?", "13"],
  ["The primary colors are red, yellow and:", "blue"],
  ["Hot is to red as cold is to:", "blue"]
];
var guesses = [];
var counter = 0;
var correctList = "<ol>", 
    incorrectList = "<ol>";

for (var i = 0; questions[0].length >= i; i++) {
 guesses.push(prompt(questions[i][0]));

  if (guesses[i] === questions[i][1]) {
    correctList += "<li>" + questions[i][0] + "</li>";
    counter++;
  } else if (guesses[i] !== questions[i][1]) {
    incorrectList += "<li>" + questions[i][0] + "</li>";
  }
}

correctList += "</ol>";
incorrectList += "</ol>";

print("<p>You got " + counter + " question right!</p>");

print("<p>You got these questions right: </p>");
print(correctList);

print("<p>You got these questions wrong: </p>");
print(incorrectList);