Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2 Solution

arr.length:

how does arr have a length? It seems to me that this is not an array. If it is an array, what data is in it and how did it get filled with data?

2 Answers

Are you referring to this section of the code?

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

If so, the contents of arr (and hence the length) are being created at the end of the file, when you call buildList:

html += '<h2>You got these right: </h2>';
html += buildList(correct);

buildList is using the 'correct' array as its argument, which starts out empty:

var correct = [];

and is populated by the for loop:

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

Yes, exactly, Thank You.