Skip to content

Instantly share code, notes, and snippets.

@BriceShatzer
Last active January 11, 2017 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BriceShatzer/8453b020099e867a5b2a44f994cbfa76 to your computer and use it in GitHub Desktop.
Save BriceShatzer/8453b020099e867a5b2a44f994cbfa76 to your computer and use it in GitHub Desktop.
js answer explanation

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

  1. max( [7,12,5] ) is called, creating an unique instance of that function's execution
  2. In that unique instance that was created, the array [7,12,5] is substituted for the variable numbers throughout the function
  3. var currentMax = [7,12,5][0]; is executed, which creates the variable currentMax and gives it a value of 7
  4. 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.
  5. The initialization instructions we provided with the for statement is run. This creates a the variable i and gives it a value of 0
  6. 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. Since i currently has a value of 0 and the number of items in our array (called its "length") is 3, the statement 0 <= 3 is true. That means the loop can continue into the statement.
  7. The statement is now executed, which starts by asking the question [7,12,5][i] > currentMax. Since i currently has a value of 0 and currentMax currently has a value of 7, the question that is being asked gets simplified to 7 > 7. This is false. That means we can continue on without needing to do anything else.
  8. 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.
  9. The instructions of that we provided for the "final-expression" (i++) was to increment the value of i by 1. That means the value of i increases from 0 to 1
  10. 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. Since i currently has a value of 1 and the number of items in our array is still 3, the statement 1 <= 3 is true. That means the loop can continue into the statement.
  11. The same question that was asked the first time, is asked again [7,12,5][i] > currentMax. Since i was incremented and now currently has a value of 1 that means we are getting the second item in the array which is 12. The variable currentMax still has a value of 7, the question that is is being asked gets simplified to 12 > 7. This is true! That means we need execute the function that was provided within the brackets of the if statement.
  12. The function found inside the if statement is currentMax = [7,12,5][i]. i still has a value of 1 so [7,12,5][i] simplifies to 12 again, which in turn simplifies the function to we are working on to currentMax = 12. This means that we are overwriting whatever the current value of the variable currentMax with the value of 12.
  13. Since there is nothing else that needs to be done inside the if statement, we return back up a level to the loop statement.
  14. 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 of i by 1. That means the value of i increases from 1 to 2
  15. 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. Since i currently has a value of 2 and the number of items in our array is still 3, the statement 2 <= 3 is true. That means the loop can continue into the statement.
  16. The statement is now executed, which starts by asking the question [7,12,5][i] > currentMax. Since i currently has a value of 2 and currentMax currently has a value of 5, the question that is being asked gets simplified to 5 > 12. This is false. That means we can continue on without needing to do anything else.
  17. 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 of i by 1. That means the value of i increases from 2 to 3.
  18. 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. Since i currently has a value of 3 and the number of items in our array is still 3, the statement 3 <= 3 is true. That means the loop can continue into the statement.
  19. [7,12,5][i] > currentMax is asked again. Since i was incremented and now currently has a value of 3 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 variable currentMax still has a value of 12, so the question that is being asked gets simplified to undefined > 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 the i <= numbers.length part was supposed to be i < numbers.length, to prevent checking an undefined value ).
  20. 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 of i by 1. That means the value of i increases from 3 to 4.
  21. 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. Since i currently has a value of 4 and the number of items in our array is still 3, the statement 4 <= 3 is false. A false statement means we are done with the loop.
  22. Since we are done and out of the loop we move on to the next thing we can do within the max() function, which is return currentMax. The return command ends the execution of the function and return any value that is specified. currentMax has a value of 12, so the function concludes and returns the value of 12.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment