Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Math.max not filtering an array by highest number

I am trying to return an array containing the highest number of each nested array using the following code:

const triangles = [
      [6, 7, 10],
      [9, 3, 6],
      [6, 3, 5],
      [6, 13, 12],
      [7, 12, 8],
    ];

function test(triangles){
    return triangles.filter(triangle => Math.max(...triangle));
}

console.log(test(triangles));

It is not filtering anything and simply returning the same values back but I don’t understand why.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to map each (sides) array within the outer (triangles) array, to a max side.

const triangles = [
  [6,  7, 10],
  [9,  3,  6],
  [6,  3,  5],
  [6, 13, 12],
  [7, 12,  8],
];

const test = (triangles) => triangles.map(sides => Math.max(...sides));

console.log(test(triangles)); // [10, 9, 6, 13, 12]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading