There are times that you need to loop through all the elements of an array to do something with each element. The best loop for dealing with arrays is the for loop. Let's take a look at an example that would require using the for loop. I'm going to jump to sublime. Notice I already have an array set up here, it's grades with a number of different grades entered in. Now let's assume that this array was established over time as grades were entered, or it could be in one setting.
But anyway, there's multiple grades entered and the array is already established. Well, we want to find out what the average of all of the grades are. And so what we need to do is we need to go through each grade and we need to sum that to the total and then we need to divide that To total by the number of grades provided, and that will give us an average. So let's look at how we would do that. So first, let's create ourselves a couple of variables. Total is going to be the sum of all the grades, I'm going to set that to zero right now.
And then average. I'm going to set that to zero as well, since we haven't computed an average at this point. All right, let's write our for loop. For I'm going to declare I, as our variable which is pretty standard, set that equal to zero. Now Now we have the condition. When do we want to break out of this loop, it's after we have access to every single element in the array.
So if we start is zero, that will allow us to access the first element. So As AI is greater than the number of elements in the array then we should break out of the loop. So we want to run this loop while i is less than grades dot length. As soon as I becomes the number that is equal to the length, that means we have gone through every element in the array because the array is zero based. Alright and finally we need to increment i plus plus. Okay, now what are we going to do in this loop?
Well, we want to sum all of these into total. So we can use one of the assignment operators to do that the plus equal assignment operator. So total plus equal grades and then in brackets we're going to put I because I will cycle through the each position for each element in that array. And plus equal says take what we already have in total and add the next element to it. So that will give us an entire total once the loop is done. Then once the loop is done, let's figure out what our average is equal to total divided by grades dot length.
Because the length is the number of items that we have in our loop that we have in our array, and finally, let's print that out. The average rate is and then let's go ahead and concatenate AV even though AV is the number because we're using the concatenation operator to a string it will coerce it to a string and print it out. We'll save that and let's see what our average is. Go ahead and copy the file path. And let's open the console. The average grade is seven 4.81818181.
Okay, so that's our average. Now obviously, we could use the math object if we wanted to trim this down a bit, and not display all the decimal places. But this gives us a good average of the grade. And that shows us how we can loop through the elements of array. Whenever you need to loop through all the elements in array. You use the for loop and use the length property of that array to determine when you have completed accessing each element in in the array.
Alright, let's move on to the next topic.