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

When using using reduce with ternary inside a map, I seem to have troubles

this is to return an array of numbers that are the highest value of the arrays inside of the base array. I can get it to work when i use for statements. But I have tried to simplify it, and can’t work out why it doesn’t work. any help would be appriciated.

function largestOfFour(arr) {
  return arr.map((x) => x.reduce((a, c) =>  c > a ? c : a, 0));
}

let test = [[1, 2, 3][4, 5, 6][7, 8, 9]]
console.log(largestOfFour(test)) // expected output[3, 6, 9] the largest number from each array

>Solution :

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

If you have values smaller than zero, you need to remove the start value

x.reduce((a, c) =>  c > a ? c : a, 0)
                                   ^

or use a very small start value, like -Number.MAX_VALUE.

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