Confusion in Loop lessons

okay so I’m going through the section on loops in the intro to javascript, I have spent like a day and a half trying to figure out these two statements, and I’m expecting this to ultimantly be something worthless that I was supposed to gloss over. but what the does this mean? is saying the arrays are zero-indexed mean that the first number in an array is 0 not 1? I don’t understand why the .length needs to be attached to a loop anyways, so I don’t know what the access to go to far.

"Remember that arrays are zero-indexed, the index of the last element of an array is equivalent to the length of that array minus 1. If we tried to access an element at the index of animals.length we will have gone too far!"

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

an array is essentially a list. Each element in the array/list has a index/position:

['a', 'b', 'c']

we humans would say that a is at position one/the first position.

however, arrays are zero indexed based, so to get the “first” element of an array, we would have to do:

const myArray = ['a', 'b', 'c'];
console.log(myArray[0]); // will log: a

so I was right? its just saying that in your example a = 0 not a = 1, because the wording in the lesson is terrible then.

exactly.

there some good points here, if we want to get the last element of an array we can do: myArray[myArray.length - 1 ]

are you re-iterating a point or are you just trying to confuse me further? because I had a handle on what the statement said but now your subtracting ■■■■ and repeating keywords and I have no clue how that would fit into the earlier statement.

No, I added a code sample to clarify things. How else are you going to get the last element of an array?

Going right back to the beginning and working up to how it applies to the loop may help.

In an array you have what is known as the index, which is a number that tells you the position of something in the array. It is zero indexed meaning the first item is at index position 0, not 1 as you may expect. The index will go up like normal counting, just starting from 0, like so:

["a", "b", "c"]
  0 ,  1 ,  2  <--- index numbers

Why are the index numbers relevant? Because you can directly get an item in an array of you know it’s index. In the example above getting the item at index position 1 would get us “b”. Another case is if you want to split arrays you need to know the index position of the place you want to split the array.

A common case is wanting to get the last item (or maybe the last X items) from an array, but you are likely not going to know the length of the array ahead of time. This is why you see array.length coming up so much, it will tell you how many items there are in the array so you can figure out the last index. But there is one thing that is easy to overlook, length is how many items there are in the array, not the index position of the last times. So when there is 1 item it will return 1, but the index of that item is 0. So the last item’s index is the length of the array - 1.

Hopefully that has all made sense. Now we come to how does this apply to loops? In JavaScript, if you want to loop over the items in an array, one way is by actually looping through the numbers that correspond to the index numbers in the array and then getting the actual item from the array with the index number. Like so:

for(let index = 0; index < array.length; index++) {
    console.log(array[index]);
}

There is a lot going on there, so let’s break it down. It may help to think of the parts that define the loop like so:

  • A starting state. In this example let index = 0, we are creating a variable index and setting the starting value to 0.
  • A condition that will stop the loop. In this example index < array.length, we are saying keep doing this loop so long as the variable index has a value less than the number of items that are in the array.
  • Something to do at the end of each loop. In this example index++, we are going to increase the value of index by one each time.
  • The code to run on each iteration of the loop. In this example, console.log(array[index]), we will get the item in the array that is equal to the value of index for that iteration of the loop.

One thing to note is index < array.legnth means the max value of index will be one less than the number of items in the array and so the index number of the last item.

1 Like

I think the confusion here is that I’m not sure if this is supposed to be a literal block of code and is just missing syntax context for me to associate it together or is this like an example saying that if we look at he ["a", "b", "c"] example the “c” would not be index as 3 but would be indexed as 2 because “a” is 0 not 1?

and the wording of the

statement I think is confusing me because if were looking at the index of animals.length why would it have gone to far? like looking at the index is inherently what a loop is for right?

Well, using your array:

const myArray = ["a", "b", "c"];

console.log(myArray[myArray.length - 1 ])

we can see the example come alive even more. My code sample was generic, it would for any array. But I had to choice a variable name for the array

its this statement that’s confusing me or really most specifically

is really the wrench in this because everything you said makes sense and I understand why it works, I don’t understand how it applies to this statment?

no offense I think our brains just don’t work the same way, because every statement you make makes less sense than the last to me.

okay this may also be another question, but if the .length function measures the number of items in the array, but the numbers in the index start with 0 not 1 does the measurement in the .length function not work the same way?

So if we have an array that has 5 items in it array.length will return 5.

const array = ["a", "b", "c", "d", "e"];
console.log(array.length); // will print 5

But the index of the 5th item is 4 not 5. So the statement means if you try to get the last item’s index using array.length you will get a number that is higher than the last item’s index by 1, hence you have gone too far.

No, arrays are zero-indexed based (counting starts at zero), while length is more human like and starts counting at one

in this example:

I deliberately subtract one to compensate for this counting difference and to prevent we go to far/out of range/out of indexes.

I think you just need to have that Eureka (source) moment/effect, once it clicks, things will become a lot clearer.