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 :
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.