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 2

Nicholas Woods
Nicholas Woods
6,260 Points

Why is it printing a list with every second item blank? I cannot figure this one out.

My code is adding items to the lists somehow and I need help figuring out why.

quiz.js
var asking;
var correct = 0;
var incorrect = 0;
var assessment;  
var qAndAs = [
  ['Who is the All Blacks current captain?', 'richie mccaw'],
  ['What rank are the All Blacks?', 'first'],
  ['Where is the next world cup held?', 'england'],
  ['Who was the previous All Blacks captain?', 'tana umanga'],
  ['Who won the last world cup?', 'all blacks']
];
var right = [];
var wrong = [];

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

function wRList(arr) {
  var wRHTML = '<ol>';
  for (var i = 0; i < arr.length; i +=1) {
    wRHTML += '<li>' + arr[i] + '<li>';
  }
    wRHTML += '</ol>';
    return wRHTML;
}

for ( var i = 0; i < qAndAs.length; i += 1) {
  asking = prompt(qAndAs[i][0]);
  asking = asking.toLowerCase();
  if ( asking === qAndAs[i][1]) {
      correct += 1;
      right.push(qAndAs[i][0]);
    } else {
      incorrect += 1;
      wrong.push(qAndAs[i][0]);
    }
} 

assessment = '<h1>Here is how well you did.</h2>';
assessment += '<p>You answered ' + correct + ' correct and ' + incorrect + ' incorrect.</p>';
assessment += '<h2>You correctly answered.</h2>' + wRList(right);
assessment += '<h2>You incorrectly answered.</h2>' + wRList(wrong);
print(assessment);

2 Answers

Hey nicholas Woods,

The only reason why you are getting all of those extra li statements is that you didn't close the li statements during your wRList function so it was adding two list items for every one that you wanted. Here is the line that needed fixed:

wRHTML += '<li>' + arr[i] + '</li>';

And now it outputs the right amount of list items.

Nicholas Woods
Nicholas Woods
6,260 Points

Facepalm! Thanks Marcus. I think when you sit in front of a computer too long you miss the obvious. I was looking for something more difficult than that.

I'm glad I could help out haha If you wanna go ahead and mark it as best answer, we'll resolve this question! :)