Doubt with iterating through an Array and its statement

Doubt with iterating through an Array and its statement
0

#1

First of all, this is not a “help with the solution” question, the challenge is already done. But I want to know if I´m wrong, or if I´m not undestanding something about arrays and how we count them.

I was at “Iterate Through an Array with a For Loop” challenge, and I was confused for a long time with the statement. You say:

var arr = [10,9,8,7,6];
for (var i=0; i < arr.length; i++) {
console.log(arr[i]);
}

Remember that Arrays have zero-based numbering, which means the last index of the array is length - 1. Our condition for this loop is i < arr.length, which stops when i is at length - 1.

If I understood well the theory about arrays, we read them from left to right, and -1 position is never reached, because it starts counting from 0 to 4. So, why are you mentioning that length -1 condition? For me, it would have sense if

var arr = [10,9,8,7,6];
for (var i=4; i >= 0; i--) {
console.log(arr[i]);
}

So in this case, we are counting them from right to left, and -1 has sense, because it stops at this point. But I don´t understand how it works with the .length property. Could anybody explain a little?


#2

You can read arrays from left to right or right to left In you last code here:

var arr = [10,9,8,7,6];
for (var i=4; i >= 0; i--) {
  console.log(arr[i]);
}

you were reading right to left (or end to beginning). You declared variable i = 4. Instead you could have used:

var arr = [10,9,8,7,6];
for (var i = arr.length -1; i >= 0; i--) {
  console.log(arr[i]);
}

This way, you do not have to manually count the number of elements and subtract one for your code to work. Hope this clears up why you would need to you arr.length -1.


#3

Is okay! Your proposal looks a better option… counting by hand it´s inefficient. But now I want to find out the reason why FCC is saying that its loop stops at -1 with that code. I think it never uses that value because it goes from zero up.


#4

Here is the statement you are asking about:

Remember that Arrays have zero-based numbering, which means the last index of the array is length - 1. Our condition for this loop is i < arr.length, which stops when i is at length - 1.

It does not say the loop stops when i is -1 for the example. It says the loop stops when i is at length - 1. It is implied this means the same thing as saying the loop stops when i is at (length - 1). Does that make more sense to you now?


#5

an array is an indexed collection of values

let a=[10, 9, 8, 7, 6]

a has 5 values - indexing starts at 0 so the last index is 4 - the size of the array is its length property so a.length is 5 - so the last index is a.length - 1 - there is no -1 index - there are no negative indexes - this is what it is

let a=[/* index 0 */ 10,
       /* index 1 */ 9,
       /* index 2 */ 8,
       /* index 3 */ 7,
       /* index 4 */ 6]

how you access the array is independent of the indexing - you can access the array left to right by starting at index 0 and increasing the index till 4 - you can access the array right to left by starting at index 4 and decreasing till 0 - you can access the array first by increasing even indexes then by decreasing odd indexes like so

for(let i=0, sign=1; i >= 0 && i < a.length; i+=2*sign) {
  console.log(`a[${i}] ${a[i]}`)
  if(i%2 === 0 && i+2 >= a.length) {
    ++i
    sign=-1
  }
}

prints

a[0] 10
a[2] 8
a[4] 6
a[3] 7
a[1] 9

#6

@rmdawson71 @ppc Yeeesss you both had explained it! I see the sense, thanks guys!


#7

@rmdawson71 :raised_hand: