Return Largest Numbers in Arrays in JavaScriptAlgorithms Section

Tell us what’s happening:
I can’t understand why its not working. Any Help?

Your code so far


function largestOfFour(arr) {
  // You can do this!
  var arrM  = [];
  
  let max = arr[0][0];
  for(var i=0;i<arr.length;++i)
  {
    for(var j=0;j<arr[i].length;++j)
    {
      if(arr[i][j] > max)
        max = arr[i][j];
      arrM.push(max);
    }
  }
  return arrM;
}

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 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays

You set max to 0 then push if arr[i][j] is over max. They’re all over max so they all get pushed to the array.

But you never remove the lower ones you already pushed. Maybe a different method other than push()?
-J

I take that back - push() is fine. You just need to move that line so it doesn’t happen on every iteration.
-J

1 Like

3 things that I see:

First of all, note the difference between the incremental operators i++ and ++i.
You use ++i and ++j in the statement of your for-loops.
++i will first increment the value and then use it. i++ will use the value and then increment it.
Check out this Medium post for more info: ++i vs. i++ in JavaScript.

This means that your loops are actually beginning from 1 instead of 0, because you are setting var i/j to zero and then incrementing it immediately. So you’re entirely missing the first value of each subarray.
You want to use i++ here.

Secondly, you’re setting your initial max value outside of both for-loops and thus outside of the entire array of arrays, meaning that the max never gets reset for each subarray of which you want to find the max value.
When your loops move on to the next subarray, you are comparing the new array’s values to the max value from the last array instead of comparing to the first value within their own array.
This means that an entire subarray could be skipped if the max value from the last array is greater than any of the new array’s values.
You MUST reset your max before looping through a new subarray - i.e. inside your first for-loop.

Last of all, your code here is problematic:

 if(arr[i][j] > max)
        max = arr[i][j];
      arrM.push(max);

You’re adding to your result array every time you find a value bigger than your current max, instead of when you find the actual max of each subarray.

This means for an array like [32, 35, 37, 39], every one of those values is being added to your result array, including 32 because 32 is being compared to 27, the max value from the previous subarray (which is why you must reset your max value for each subarray before looping through it).
You want to finish looping through the subarray, updating max as you go and only push to your result array once you have looped through the entire subarray.

Hope that helps out!

1 Like