Return Largest Numbers in Arrays - Order of Array?

Return Largest Numbers in Arrays - Order of Array?


Hi there. Here is my code

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

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

My result is [5, 27, 39, 1001]. It says it should return [27,5,39,1001]. I don’t understand why I should be getting it in that weird order?? I’m so confused


I copied your code to and for the three tests, I always get [0, 0, 0, 0].

If you looked closely there’s a really subtle error with your syntax. Look at the inner loop’s header. After you find and fix that, I believe you only have to deal with some logic error with your code, and you’re done!


Thanks. I found the small error. For the logic error, is it a big thing? Do i have the general idea right?


Have you solved it yet? You have the general idea right. You’re close :slight_smile:


I’ll be honest. I kinda cheated and used the math.max function and got it. But i’m desperate to know what I did wrong :grimacing: :grimacing:


Nothing wrong with using Math.max. :slight_smile:

You just had to move var comp1 = 0; just before the inner loop starts. You only want to get the max number per subarray, so it makes sense to reset it before looping through a subarray.


Thank you! That’s what I was missing!