Why my code not working with Basic Algorithm Scripting: Return Largest Numbers in Arrays using filter!

Tell us what’s happening:
it successful with me using the map …look at the end of code /Done using map/
but I’m trying to solve it using filter …it didn’t work!.
why?

Your code so far


function largestOfFour(arr) {
 let y = [];let z=0;
for(let i = 0 ; i < arr.length ; i++)
 { 
     Math.max(arr[i].filter(num => {
       if(num>z){z=num}
     }))
 }  
 return z;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
/* let x = [];
 for(let i = 0 ; i < arr.length ; i++)//
   {

   x.push(Math.max(...arr[i].map(num => num)));
   }
   return x;*/

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

It’s telling you what the problem is. The first test reports:

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

should return an array.

But you are not retuning an array, you’re returning a number.

If you read the instructions:

Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

So, you should return and array of four numbers, the largest number in each subarray.

this is unnecessary, you are not making any change so map is just consuming resources without adding any functionality, you could just ...arr[i]

1 Like