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

In the loop for buildList(arr), where does the arr.length come from since it was never declared as a variable above?

In the function buldList(arr), the for loop is set to run as i < arr.length. However, arr was never set as a variable of any kind so how does the program know that it's an array or any other kind of variable?

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

I didn't include the entire function because this is the only par that confused me.

1 Answer

pi R
pi R
12,720 Points

Hi ! arr is an argument of the function! so arr.length will be the length of the argument you pass to buildList(arr);

example : var arrayOne = [];

buildList(arrayOne) will do : for ( var i=0; i < arrayOne.length; i +=1) { listHTML += '' + arrayOne[i] + ''; }

It's also important to note that arrays aren't the only types that have a length property. If a string was passed to this function, it would still work, but you would get a weird list of each character (including spaces and punctuation) in its own list item.