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

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.