I am trying to return the max value of an array in JavaScript without using the Math.max
function. Is reduce
a good way to do this? My problem is that it is returning 5
not 11
like I want it to. Am I going about this the best way? Could somebody guide me in the right direction.
Here is what I have so far:
function max(arr) {
return arr.reduce(function(prev, curr) {
return (prev.value >= curr.value) ? prev : curr;
});
}
console.log(
max([3, 1, 2, 7, 11, 3, 4, 5])
);