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

Darien Kim
Darien Kim
3,126 Points

can anyone explain, why ListHTML += "</ol>" why not = "<ol>" Thanks

can anyone explain, why ListHTML += "</ol>" why not = "<ol>" Thanks

and why does it have to add [] for variations? var correct = []; var wrong = [];

also my code has undefined on top , I cant find why it does that.

here is my code

var questions = [ ['How many states are in the United States?', 50], ['How many continents are there?', 7], ['How many legs does an insect have?', 6] ]; var correctAnswers = 0; var question; var answer; var response; var correct = []; var wrong = []; var html;

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

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

for (var i = 0; i < questions.length; i += 1) { question = questions[i][0]; answer = questions[i][1]; response = parseInt(prompt(question));

if (response === answer) {

correct.push(question);

} else { wrong.push(question); } }

html += "<h2>you got these question right</h2>"; html += buildList(correct); html += "<h2>you got these questions wrong</h2>" html += buildList(wrong);

print(html);

Felix Sonnenholzer
Felix Sonnenholzer
14,654 Points

Could you attach a snapshot of your workspace please?

2 Answers

Tom Sager
Tom Sager
18,987 Points

ListHTML = "abc" sets the value of ListHTML to "abc". ListHTML += "def" takes the existing value of ListHTML and appends the string to the end. It is a shorthand for ListHTML = ListHTML + "def".

ListHTML = "";     # value is ""
ListHTML = "abc";  # value is "abc"
ListHTML += "def"; # value is "abcdef"

Instead of just counting the correct and wrong answers, we want to save the questions associated with the right and wrong answers. Each question is a string. These string are stored in two arrays, named 'correct' and 'wrong'. The [] at the end of the variable declaration tell us that this is an array.

question = 'How many states are in the United States?'
var correct = [];   # creates an empty array
correct.push(question)
    # now there is one element in the array:
    # [ 'How many states are in the United States?' ]

correct.push(question)
    # now there are two elements in the array:
    # [ 'How many states are in the United States?', 'How many states are in the United States?' ]

I am not sure what you mean by "undefined on top".

Darien Kim
Darien Kim
3,126 Points

Thank you for your explanation, I relaunched Workspace and the unidentified doesn't appear now.