0

I'm trying to understand nested forloops and I found this code that should be simple and I want you to make sure I understand it.

var maximum = function(arr){
var out;
for(var i = 0; i < arr.length; i++){
     out = true;
    console.log(i)
    for(var j = 0; j < arr.length; j++){

        if(arr[i] < arr[j]){
            // console.log(i, j)
            out = false;
             // console.log(arr[i], arr[j], " out ", out)
        }
    }
    if(out) return arr[i];
}
// return null;
}
 console.log(maximum([1,2,5]))

first of all i'm not sure why they define out = true but I wanted to undestand the looping right now

so for the first iteration

i is set to 0 and out is set to true then we go into the second for loop

i= 0

inside the second forloop: for(var j = 0; j < arr.length; j++){

we test (arr[i] < arr[j]) 3 time for the length of the array

1st test in the inner loop :arr[0] = 1 < arr[0] = 1 ==false (so we do nothing? shouldn't out remain true and we return if(out) return arr[i];?)

2nd test in the inner loop arr[0] = 1 < arr[1] = 2 == true.. set out to false

3rd test in the inner loop arr[0] = 1 < arr[2] = 5 == true .. set out to false

then we go to make i 1

then we test is arr[i] = 2 < arr[j] which is 1 ? false we dont set out to false

is arr[i] = 2 < arr[j] which is 2 ? false we dont set out to false

is arr[i] = 2 < arr[j] which is 5? true we set out to true

then we set i to 2 arr[2] = 5 is arr[2] which is 5 < arr[0] which is 1 this is false so out remains true

is arr[2] which is 5 < arr[1] which is 2 this is false so out remains true

is arr[2] which is 5 < arr[2] which is 5 this is false so out remains true

Is the corrected process for the nested loops?

jack blank
  • 5,073
  • 7
  • 41
  • 73
  • This is horrible code. You should throw it away and find other code to learn from. –  Dec 03 '15 at 04:30

3 Answers3

1

Yes you've got the gist of it. The inner loop (the one that uses j as the counter variable) will run three times for every one time the outer loop ( the i loop) runs.

A simpler way to understand this is to run this code:

for (var i = 0; i < 3; i += 1){
    console.log("outer loop")
    for (var j = 0; j < 3; j += 1){
        console.log("inner loop")
    }
}

This allows you to focus just on the nested loops and not on trip-wire variables and such that are in the original code you posted.

branweb
  • 171
  • 7
0

To answer your first question: first of all i'm not sure why they define out = true -- this is just an initialization which gets reset on each iteration of the outer loop. If that value is the max, then out will remain true -- otherwise it will be false.

To answer your second question: 1st test in the inner loop :arr[0] = 1 < arr[0] = 1 ==false (so we do nothing? shouldn't out remain true and we return if(out) return arr[i];?)

Yes, you have the test correct -- the test is false so out remains true, but it doesn't drop down to the if(out) statement until the inner loop has completed its iterations.

To summarize the looping: the inner loop compares the current value from the outer loop against all of the other values (including itself). The outer loop is moving through each value (for a total of 3 times).

devlin carnate
  • 8,309
  • 7
  • 48
  • 82
  • Thanks for your response. This part `1st test in the inner loop :arr[0] = 1 < arr[0] = 1 ==false (so we do nothing? shouldn't out remain true and we return if(out) return arr[i];?)` you said : "but it doesn't drop down to the if(out) statement until the inner loop has completed its iterations." I'm thinking now that `out` doesnt become true because the next tests are false, right? and that makes `out` remain true because we initialized at the top.. lets say we test the last one where i = 2 which is the value 5 that fails all tests and out still remains true? that's how this works? – jack blank Dec 03 '15 at 03:44
  • in the first loop, where the comparison is 1 < 1, out remains true. however, on the next loop (and the one following), out starts out as true but is then set to false. in the final outer loop where i = 2, the last test is 5 < 5, so out is does not get set to false and remains true. – devlin carnate Dec 03 '15 at 15:36
0

Short answer: Yes you are right.

Long answer:

In a nut shell this function is trying to find the maximum in an array so it sets two for loops. i is the index for the first number and j is the index for the second number it's comparing i against.

The purpose of the out is simply to make sure it does not have to continue the outer loop if it finds it has the maximum value at j. It does a redundant check as you pointed out when i and j are the same value because it's, well, the same array and therefore the same number. Ideally it doesn't need to make this check, but whatever.

I think the biggest optimization that can be made as you may have noticed is that when out is set to false, does it really need to continue? We know it's not the max anymore so why bother? You can solve this really simply by making the inner loop check the value of out and break out

for(var i = 0; i < arr.length; i++){
     out = true;
    console.log(i)
    for(var j = 0; j < arr.length && out; j++){

        if(arr[i] < arr[j]){
            // console.log(i, j)
            out = false;
             // console.log(arr[i], arr[j], " out ", out)
        }
    }
    if(out) return arr[i];
}

Hope this helps.

Community
  • 1
  • 1
aug
  • 11,138
  • 9
  • 72
  • 93
  • I see this gives the same result as the code in the question.I don't see a difference. can you show me with logs maybe how when `ou`t is set to false in the inner loop and when the outer `out`.is set to false. the outer loop doesn't need to continue. when you say `does it really need to continue?` are you referring to do more inner loops. what do you mean? – jack blank Dec 03 '15 at 04:04