Help me! I'm strucked

Tell us what’s happening:

when I try to run this code ,the output that I am getting was [25, 48, 21, 0] instead of [25, 48, 21, -3]. Help me ,fix the code to get required output.

Your code so far


function largestOfFour(arr) {
let new_arr=[],max=0;
for(var i=0;i<arr.length;i++){
  for(var j =0;j<arr[i].length;j++){
    if(max<=arr[i][j]){
        max = arr[i][j];
    }
  }
  new_arr.push(max);
  max=0;
}
console.log(new_arr);
return new_arr;
}

largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

if max is more than arr[i][j] the function skips and returns 0
For any element in the last array that’s what you set :slight_smile:

This is not a solution but max=-100 should work.

Hello;

instead of i and j loop only with i and use the predefined function Math.max() and apply it on every arr[i]!
It works! (Trick: use also the spread operator(…))!
If you need more help don’t hesitate!