Return the largest number in the arrays. Help me please

function largestOfFour(arr) {
var str=[];
  for(var i=0; i<arr.length; i++){
    var maxNum = 0;
      for(var j=0; j<arr[i].length; j++){
        if(arr[i][j]>maxNum){
          maxNum = arr[i][j];
        } else if(arr[i][j]<0){
            var minNum = arr[i][j];
            if(arr[i][j] >= minNum){
              maxNum = arr[i][j];
            }
        }
      }
    str.push(maxNum);
  }
  return str;
}

console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]])); //returns [5, 27, 39, 1001]
console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]])) // returns [25,48,21,-10]

In the last function call It should return [25, 48, 21, -3]
Can anyone correct the above code to find the largest number.

Hello there.

Do you have a question?

If so, please edit your post to include it.

The more information you give us, the more likely we are to be able to help.

I want to correct the code. And the problems are edited in my post. My question is the last function call is wrong.

The bug, I believe, is here.

Walk me through the logic here. What do you think this code is doing? :slightly_smiling_face:

@nhcarrigan is right.

console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]])) // returns [25,48,21,-10]

All three other tests pass when running this code. So there is something different about this last test causing your problem. Can you identify what is different about the numbers this last test passes to the function that might cause your issue?

I don’t think you need this arr[i].lenght, you can use just arr[i].

That would break the loop conditions, I believe. That line seems correct, to me.