For loop inside for loop questions

Hey it’s my second time going through these exercises and I knew I had to nest a for loop but without looking at the solution too much I thought I’d just post this.

Seems my biggest issue is with how to run through a nested array with a nested for loop. I saw the answer has a different variable name etc. Can you explain?

Update: Thought it may be better to post the parts of answer I don’t understand in the code:

function largestOfFour(arr) {
  var results = [];
  for (var i = 0; i < arr.length; i++) {
    var largestNumber = arr[i][0]; // why do we keep this 0 here?
    for (var ii = 1; ii < arr[i].length; ii++) { // why do we start at 1 not 0?
      if (arr[i][ii] > largestNumber) {
        largestNumber = arr[i][ii];
      }
    }

    results[i] = largestNumber; //
  }

  return results;
}

Your code so far


function largestOfFour(arr) {
var largestArr = [];// initialize a variable to hold the largest value
                  
for (var i = 0; i < arr.length; i++) {
  largestArr = arr[i].length;
  for (var i = 0; i < arr[i].length; i++) // this is the part I don't understand
{ 
  if (arr[i].length > largestArr) {
    largestArr = arr[i];
  } 
}

return largestArr; // return the largest value
}

console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

what’s your question about this?

You can ignore this part

Well, the goal is to find the “biggest” in the set.
When looping the first element (the element “0’”) is of course the biggest one as we have yet to look at the others and see if that case still holds true.
But is a safe assumption even if it’s the only element in the set.

So it make sense to actually start comparing the first element (0) with the second (1), hence why starting from 1 as index.

Hope it helps :sparkles:

1 Like

what would you write there instead?

you are comparing each element of the subarray, array[i][0] is the first element in the subarray

the first element is already part of the comparison as starting value of largestNumber, you don’t need to compare it with itself

you need two different variables because you have two different indexes

instead of comparing the elements inside the subarrais, you are just comparing the lengths of the subarrays - it’s always 4

I thought the first for loop was about checking the index of outer array (starting at 0), then the second was checking the index of inner array (starting at 0)

[i] would be [4, 5, 1, 3]… and then 0 would be 4… then [13, 27, 18, 26] and 0 would be 13 right?

arr[i][ii] would be [4, 5, 1, 3] then 1 would be 5?

So 4,13,32,1000 is the first loop… Sorry is there a place to find the expanded explanation of this?

array[i] would be [4,5,1,3] and array[i][0] would be 4


you could use this tool JavaScript Tutor - Visualize JavaScript code execution to learn JavaScript online to see the code executed step by step (to see a function execute you need to include a function call)

Interesting, I don’t really understand the way that website is explaining it.

But ok so if array[i] would be [4,5,1,3] and array[i][0] would be 4 then

for (var i = 0; i < arr.length; i++) {
    var largestNumber = arr[i][0];

is checking the first index only of each sub array (4,13,32,1000), then

for (var ii = 1; ii < arr[i].length; ii++) { // why do we start at 1 not 0?
      if (arr[i][ii] > largestNumber) {
        largestNumber = arr[i][ii];

is comparing 4 to 5, 13 to 27, 32, to 35, 1000 to 1001 then storing the largest one as result and continuing for the loop?

Sorry idk why it’s so hard to get my head around this. Even if I get it after all this explanation I’m not sure how I’d replicate this…

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]])

it’s comparing 4 to 5 to 1 to 3 and picking the biggest

// i = 0

// ii = 0
let largest = 4;

// ii = 1
largest < 5
// ii = 2
largest < 1
// ii = 3
largest < 3
1 Like

For this problem, I like thinking about how I would do this by hand.

Let’s look at a single array.

[5, 2, 8, 42]

I would start at the beginning.

[5, ?, ?, ?]
biggest = 5

Then I’d look at the next number

[5, 2, ?, ?]
biggest = 5 // still

And the next

[5, 2, 8, ?]
biggest = 8 // changed!

And the last

[5, 2, 8, 42]
biggest = 42 // changed!

Your code replicates how I would do this by hand.

// The biggest is the first I see
let largestNumber = arr[0];
// Check (loop over) the rest of the numbers
for (let i = 1; i < arr.length; i++) {
 // Update the biggest seen so far if I find a bigger
 if (arr[i] > largestNumber) {
   largestNumber = arr[i];
 }
}

The problem makes this more complex by adding nested arrays, which is where the outer loop comes in.

Yeah so is because the loop is within a for loop it would not move on to the next subarray until it goes through each index of the sub array?

So

for (var i = 0; i < arr.length; i++) { // goes through each subarray
    var largestNumber = arr[i][0]; // stores 4 as largestNumber
    for (var ii = 1; ii < arr[i].length; ii++) { 
      if (arr[i][ii] > largestNumber) { // if arr[0][1] aka 5 > 4
        largestNumber = arr[i][ii]; // largestNumber = 5
      }
    }

    results[i] = largestNumber; //
  }

I think the loop part within a loop is my biggest issue. How it’s looping is probably confusing me

what’s inside a loop is executed in its entirety before going to next iteration

so this is correct

1 Like

I didn’t realize this earlier. Now it makes much more sense.

Can you you confirm if I understand this part:

ii < arr[i].length

Seems it’s while the inner loop is smaller than the length of the outer loop, so

when ii is 1 = 5
when ii is 2 = 1
when ii is 3 = 3

until ii is less than 3 (so stop at ii is 2 if outer loop is 3?)

outer loop being
0 = [4, 5, 1, 3]
1 = [13, 27, 18, 26]
2 = [32, 35, 37, 39]
3 = [1000, 1001, 857, 1]

**update Just realized it’s actually looping again within the inner array not inner within outer array (explanation video for anyone that sees this in the future: Javascript Coding Challenge #6: Largest Numbers in Arrays (Freecodecamp) - YouTube)