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 Solution

Kristin Leone
Kristin Leone
3,881 Points

Mine is slightly different and it works BUT a comma is inserted on its own line between each <li>. Why? How?

I checked the source of through chrome developer tools. It just appears on its own and isn't in its own <li>. When I click or hover, it doesn't connect with anything else. I tried it with a standalone text editor and different browsers.

I put my output as 2 <ol> within the html document. Here is the rest of the code:

function printRight(message) {
 var pRight = document.getElementById('right');
 pRight.innerHTML= message;
}

function printWrong(message) {
 var pWrong = document.getElementById('wrong');
 pWrong.innerHTML= message;
}

var quiz = [

  ['What color is the sky?', 'blue'],
  ['What color is the grass?', 'green'],
  ['What color is ketchup?', 'red']

];


var answer;
var correct = 0;
var correctArray = [];
var incorrect = 0;
var incorrectArray = [];



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

   if ( answer === quiz[i][1]) {
     correct += 1;
     correctArray.push('<li>' + quiz[i][0] + '</li>');

    } else {
      incorrect += 1;
      incorrectArray.push('<li>' + quiz[i][0] + '</li>');

    }; 

};  

printRight('<h2>Total Correct: ' + correct + '</h2>' + correctArray);
printWrong('<h2>Total Incorrect: ' + incorrect + '</h2>' + incorrectArray);

Hi Kristin Leone, I've put your code in a code block to make it easier to read.

I also used some HTML code to display the < and > symbols, since they otherwise will get converted into the actual HTML tags you were trying to mention.

2 Answers

The commas come from the array you stored the li elements in. To get rid of them, just use the .join() method on your arrays before you pass them into your print functions like so:

    printRight('Total Correct: ' + correct + '' + correctArray.join(""));
    printWrong('Total Incorrect: ' + incorrect + '' + incorrectArray.join(""));
Julian Aramburu
Julian Aramburu
11,368 Points

Hi! Could you please format your code? use ```javascript before the code in order to format it :)!