Return Largest Numbers in Arrays need help

Tell us what’s happening:
hii there…i cannot pass the test :slight_smile:
largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]) should return [25, 48, 21, -3]. pls help

Your code so far


function largestOfFour(arr) {
  // You can do this!
  let biggestarray=[0,0,0,0];
  for(let i=0;i<arr.length;i++)
  {
    for(let j=0;j<arr[i].length;j++)
    {
      if(arr[i][j]>biggestarray[i])
      {
        biggestarray[i]=arr[i][j];
      }
    }
  }
  console.log(biggestarray);
  return biggestarray;
}

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 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

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

your biggestArray should be biggestarray= [-Infinity, -Infinity, -Infinity, -Infinity], because there are negative numbers in the input array.

1 Like

thanku so much,now i get it…:grinning:)

how about this solution it gets me the same error
function largestOfFour(mainArray) {
return mainArray.map(function (subArray){
return subArray.reduce(function (previousLargestNumber, currentLargestNumber) {
return (currentLargestNumber > previousLargestNumber) ? currentLargestNumber : previousLargestNumber;
}, 0);
});
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

Remove the , 0 and you’re good.

1 Like

thank u…help much appreciated…