The two functions they provide are basically identical, with the only difference being the comparison that they are making. Hopefully talking through an example use of one, step by step will help...
So lets say we have an example array of numbers that look like this [7,12,5]
.
When we call the max()
function and pass that array of number into it like this:
max( [7,12,5] );
// or this:
var numberArray = [7,12,5];
max( numberArray );
Calling a function creates a new and unique instance of that function, with some of the variables being replaced with the things that were provied. In the case of the max()
function, numbers
gets replaced with the array we provide, making the function go from looking like this:
function max(numbers) {
var currentMax = numbers[0];
for (var i=0; i <= numbers.length; i++) {
if (numbers[i] > currentMax) {
currentMax = numbers[i];
}
}
return currentMax;
}
// to something like this
function max([7,12,5]) {
var currentMax = [7,12,5][0];
for (
var i=0;
i <= [7,12,5].length;
i++) {
if ([7,12,5][i] > currentMax) {
currentMax = [7,12,5][i];
}
}
return currentMax;
}
From there, we can walk through it step by step.
So the first thing that happens is that a variable named currentMax
is set with the value of the very first item in the array, because the function needs to start with something. Items in an array can be accessed using a number inside of square brackets ([]
), starting with 0. In our example, currentMax
is being set to whatever the value of the first item in the array, which is 7. Now that a value is set for currentMax
, the function has something it can work and now moves into the loop.
for
basically is stating that we want do a loop and defines some instructions for that loop. We can give a for loop 3 different sets of instructions.
- The first set are the "initialization" instructions. Basically, what do you want to do before you do any sort of looping? In our example, for the initialization instructions we are creating the variable
i
, and assigning it a value of 0. This will help us keep track of our progress while we are looping. - The second set are the "condition" instructions. This is the instructions that will be checked before each iteration of the loop to decide if the loop should continue. In our example, we are checking if the value stored in the variable
i
is less than or equal to(<=
) the total length of the array ([7,12,5].length
). If it is true, keep going. If it is false, stop. - The last set are the "final-expression" instructions and are completed after each iteration of the loop. In our example we are we are incrementing the value of
i
by one (i++). This is how the loop keeps track of how many times it has gone through the process.
Now that everything in the is loop ready to go, we get into the actual statement of the loop, which is pretty simple. It asks "is the value that exists at the current location in the array bigger than what I have stored for the variable currentMax
(if ([7,12,5][i] > currentMax)
)? If it is, set currentMax to that value (currentMax = [7,12,5][i];
). After that is done, the loop as reached the end of the statement so it moves on to the next iteration of the loop. Once all the iterations of the loop are completed, return whatever the final value of the variable currentMax
.
So know that we know what a single iteration of the loop looks like, lets go through what the whole thing would look like, step by step. (It's worth noting, I think the i <= numbers.length
part was supposed to be i < numbers.length
. I'll explain why, later.)
max( [7,12,5] )
is called, creating an unique instance of that function's execution- In that unique instance that was created, the array
[7,12,5]
is substituted for the variablenumbers
throughout the function var currentMax = [7,12,5][0];
is executed, which creates the variablecurrentMax
and gives it a value of7
var i=0; i <= [7,12,5].length; i++) ,
is executed, letting the computer know that we are going to be doing a loop, and provides the computer the three set of instructions that it needs.- The initialization instructions we provided with the
for
statement is run. This creates a the variablei
and gives it a value of0
- Next, the first iteration of the loop begins. The very first step is to check the conditional statement (
i <= [7,12,5].length;
) to see if we should continue. Sincei
currently has a value of0
and the number of items in our array (called its "length") is3
, the statement0 <= 3
is true. That means the loop can continue into the statement. - The statement is now executed, which starts by asking the question
[7,12,5][i] > currentMax
. Sincei
currently has a value of0
andcurrentMax
currently has a value of7
, the question that is being asked gets simplified to7 > 7
. This is false. That means we can continue on without needing to do anything else. - Since there is nothing else that needs to be done in the statement, that means the first iteration of the loop has been completed, and the "final-expression" is gets executed.
- The instructions of that we provided for the "final-expression" (
i++
) was to increment the value ofi
by1
. That means the value ofi
increases from0
to1
- Now the second iteration of the loop begins. Like before, the first step is to check the conditional statement (
i <= [7,12,5].length;
) to see if we should continue. Sincei
currently has a value of1
and the number of items in our array is still3
, the statement1 <= 3
is true. That means the loop can continue into the statement. - The same question that was asked the first time, is asked again
[7,12,5][i] > currentMax
. Sincei
was incremented and now currently has a value of1
that means we are getting the second item in the array which is12
. The variablecurrentMax
still has a value of7
, the question that is is being asked gets simplified to12 > 7
. This is true! That means we need execute the function that was provided within the brackets of theif
statement. - The function found inside the
if
statement iscurrentMax = [7,12,5][i]
.i
still has a value of1
so[7,12,5][i]
simplifies to12
again, which in turn simplifies the function to we are working on tocurrentMax = 12
. This means that we are overwriting whatever the current value of the variablecurrentMax
with the value of12
. - Since there is nothing else that needs to be done inside the
if
statement, we return back up a level to the loop statement. - and since there is nothing else that needs to be done in the loop statement, that means the second iteration of the loop has been completed, and the "final-expression" (
i++
) gets executed again, which increments the value ofi
by1
. That means the value ofi
increases from1
to2
- Now the third iteration of the loop begins. again we check the conditional statement (
i <= [7,12,5].length;
) to see if we should continue. Sincei
currently has a value of2
and the number of items in our array is still3
, the statement2 <= 3
is true. That means the loop can continue into the statement. - The statement is now executed, which starts by asking the question
[7,12,5][i] > currentMax
. Sincei
currently has a value of2
andcurrentMax
currently has a value of5
, the question that is being asked gets simplified to5 > 12
. This is false. That means we can continue on without needing to do anything else. - Since there is nothing else that needs to be done in the loop statement, that means the third iteration of the loop has been completed, and the "final-expression" (
i++
) gets executed again, which increments the value ofi
by1
. That means the value ofi
increases from2
to3
. - Now the fourth iteration of the loop begins. Like before, the first step is to check the conditional statement (
i <= [7,12,5].length;
) to see if we should continue. Sincei
currently has a value of3
and the number of items in our array is still3
, the statement3 <= 3
is true. That means the loop can continue into the statement. [7,12,5][i] > currentMax
is asked again. Sincei
was incremented and now currently has a value of3
that means we are getting the fourth item in the array. Since a fourth item has not been defined yet,undefined
is what is it used. The variablecurrentMax
still has a value of12
, so the question that is being asked gets simplified toundefined > 7
. This is false (I won't go into why.). That means we can continue on without needing to do anything else. (This is why I think thei <= numbers.length
part was supposed to bei < numbers.length
, to prevent checking an undefined value ).- Since there is nothing else that needs to be done in the loop statement, that means the third iteration of the loop has been completed, and the "final-expression" (
i++
) gets executed again, which increments the value ofi
by1
. That means the value ofi
increases from3
to4
. - Now the fifth iteration of the loop begins. Like before, the first step is to check the conditional statement (
i <= [7,12,5].length;
) to see if we should continue. Sincei
currently has a value of4
and the number of items in our array is still3
, the statement4 <= 3
is false. A false statement means we are done with the loop. - Since we are done and out of the loop we move on to the next thing we can do within the
max()
function, which isreturn currentMax
. Thereturn
command ends the execution of the function and return any value that is specified.currentMax
has a value of12
, so the function concludes and returns the value of12
.