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?
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.
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