[is this what's happening?]: Return Largest Numbers in Arrays

Hi everyone,

I’m on the exercise “Return Largest Numbers in Arrays” (link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays), and was wondering if anyone could provide feedback on my understanding of that’s happening, thanks in advance.

SPOILER ALERT: I post a modified version of the answer below.

function largestOfFour(arr) {
  const results = [];

  // Creating an outer loop that will iterate through the outer array.
  for (let i = 0; i < arr.length; i++) {

    // This variable resets every time the outer loop is iterated?
    let largestNumber = 0;
    
    // Creating an inner loop to work with the nested arrays (ie sub-arrays).
    for (let j = 1; j < arr[i].length; j++) {
      if (arr[i][j] > largestNumber) {
        largestNumber = arr[i][j];
      }
    }
    
    results[i] = largestNumber;
  }

  // This is to see what's in the array, can comment this out.
  console.log(results); 
  
  return results;
}

// Changed some of the numbers from the exercise to test different scenarios.
largestOfFour([[13, 27, 18, 26], [-4, -5, -1, -3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

I’m a bit shaky on understanding block scope and I think the key part of this exercise is where we declare the variable “largestNumber”.

  1. First off, my understanding is that each time we iterate through the outer loop, when i = 0, i = 1, i = 2, and i =3, we’re actually creating 4 separate “i” variables.

  2. On top of that, at each “i” (as we move through the outer loop), we also create 4 separate “largestNumber” variables, i.e. the “largestNumber” variable that is created when i = 1, is different from the “largestNumber” variable that is created when i = 2.

  3. As such, each time we iterate through the outer loop, the “largestNumber” variable is reset to 0.

I understand that initializing “largestNumber” to 0 is not ideal and doesn’t work if the nested array contains all negative numbers. I did this to try to see what’s happening.

Would anyone be able to provide feedback on my understanding of the exercise?

Thanks again.

Hi @blackpug !

It looks like you are close and have a basic understanding on how it works.

There are two issues that are preventing you from passing.

No.1:

As you mentioned, initializing it to zero isn’t going to work when dealing with sub arrays of negative numbers.
Maybe you could initialize it to one of the numbers in the sub array instead. (hint hint :smiley: )
I would suggest playing around with that approach.

No.2:

If j=1 then that means we are starting our count at index 1 for the sub arrays.
But that will pose a problem for this test case here.

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]])

I would suggest changing the starting count to fix that bug.

hope that helps!

Hi jwilkins.oboe,

Thanks for the reply, appreciate it.

I actually couldn’t pass the exercise after many attempts and in the end I looked at the answer :pensive:; sorry this was before even posting here.

Still, having looked at the answer, I still thought there were gaps in my understanding (perhaps big gaps) and I was really trying to understand the points I raised (points #1-3) regarding scope.

Sorry I wasn’t very clear in my first post. I generally understand the answer provided by fCC but I purposely tweaked the code to help me better “see” what’s happening regarding the scope of the variable “largestNumber” and whether or not it resets after each iteration of the outer loop. I initialized the variable to 0 so that I can see it being reset and then added to “results” when all numbers in a nested array are negative, as 0 would be larger than all of them.

I was wondering if you could provide feedback on the points (#1-3) I raised in the first post. This is just to help me understand 100% what’s happening behind the scenes. Thanks again, and also I wanted to say a while back I listened to your interview on the CodeNewbie podcast, it was really cool!

This part is true.

This part is complicated. Its the same variable but has a separate ‘binding’ on each loop iteration.


I really recommend against looking up the an swers before you finish the challenge - reading code and writing code both important but separate skill

2 Likes

As Jeremy mentioned, you are correct in these two points here. So I think you have a better understanding then you realize :+1:

You could copy and paste your code into something like vs code or another editor and run it though a debugger.
That might help you better understand how the code is working step by step with each variable.
It might also help with your understanding for your first point you made.

Thanks for listening :slight_smile:

1 Like

@JeremyLT and @jwilkins.oboe, thanks for the feedback and clarifying the points for me, appreciate it.

And point taken, I’ll try to avoid looking up the answers before solving the exercises.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.