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

Part 2 Solution The "function listitem" work improperly..

var point = 0;
var correctAnswer = [];
var wrongAnswer = [];
var question = [
  ["This is question 1","a1"],
  ["This is question 2","a2"],
  ["This is question 3","a3"]
]; 

function listitem(arr) {
  for (var i = 0 ; i < arr.length ; i += 1) {
    var listli = "<li>" + arr[i] + "</li>";
  };
  var listul = "<ul>" + listli + "</ul>";
  return listul;
};

for (var i = 0 ; i < question.length; i +=1) { 
  if (prompt (question[i][0]) === question[i][1]) {
    point += 1;
    correctAnswer.unshift(question[i][0]);    
  } else {
    wrongAnswer.unshift(question[i][0]);
  }
};

var html = "<h2>you got " + point + " question (s) right </h2>";
html += "<strong>Correct answer</strong>";
html += listitem(correctAnswer);
html += "<strong>Wrong answer</strong>";
html += listitem(wrongAnswer);

document.getElementsByClassName("text")[0].innerHTML = html;

1 Answer

Lee Vaughn
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Lee Vaughn
Treehouse Teacher

There are a few different issues with your function.

First, you need to declare your variables before (and outside) the for loop. Inside the loop, you want to use "+=" to add to the listli variable each time it iterates through the array.

Check out the video at the 1:41 mark for a walkthrough of this function.

Just a suggestion, but you might also check your camelCasing or more specifically lack of camelCasing. For example, your function would typically be typed as "listItem" and your variable names would be "listLi" and "listUl". Not using camelCasing doesn't necessarily cause errors with your code but it can make it more difficult to read.

The same logic applies to spacing and indentation. Your code is very difficult to read and understand as it is currently written.