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 Arrays Loop Through Arrays Loop Through an Array

for loop problem

let i = 0, i < arr.length; i++ so here i is 0 and the arr parameter is 1? because of the length (the number of parameters used is 1)? so the condition is true? and program runs.

is that right?

what is arr[i]?
and yes i have done previous course but this is always difficult for me

This first statement of this loop is pretty clear: set a variable, i, equal to zero.

The second statement of this loop (i < arr.length) is not a Boolean (true or false) value. The arr.length will be an integer equal to the number of items in the array.

The third statement of this loop determines what to do to the variable, i, each time the loop is executed. In this case, run the code, then increase i by one.

So, this loop will run a number of times equal to the number of items in 'arr'

This statement isn't deciding whether or not this loop should run, it is deciding how many times this loop will run.

Lastly, arr[i] will change each time the loop is executed. For example, the first time through the loop, arr[i] will be equal to the first value in the array 'arr' or arr[0].

if i = 10; i < arr.length; i++ wouldnt run because the condition isnt true (number 0f elements in array is 4). this isnt a boolean type program no i agree but i < arr.length is false otherwise it would run.

arr[i]? is going through the array and selecting a different element each time?

1 Answer

Correct: if i=10 and arr.length = 4, the loop will never run; i begins greater than arr.length, so i < arr.length will be false and the loop will not run.

In the case provided in the video, the variable i is representing the number of times the loop has already finished running, so it starts at 0. We want the loop to run for each item in the array, so once the loop has completed four times, stop running the loop. To do this, we tell the loop to run only while i < arr.length and then increment i and the end of each iteration of the loop. After four times through the loop, i equals 4. Arr.length also equals four, and since 4 is not less than 4, the loop stops.

This pairs well with the way array items are indexed. The first item in an array is indexed at 0, and referenced like arr[0]. So, the first time through the loop where i = 0, arr[i] is the same as arr[0]. The second time through the loop, i = 1, and arr[i] = arr[1]. This continues until the loop ends. This is how arr[i] is able to select a different element each time, since i is different through each loop iteration.

I hope that doesn't sound like nonsense.

No its not nonsense, it sounds great thanks for your time :-) really helped alot!