I need a clean way of finding max
for an array in JavaScript. Say it is arrayMax
, then:
arrayMax([]) // => 0
arrayMax([1], [2]) // => 2
arrayMax([-1]) // => -1
What I've tried:
Math.max.apply(null, [1,2,3]) // => 3
But it doesn't work for:
Math.max.apply(null, []) // => -Infinity
Note that it's not an duplication with this question since I want the empty array to return 0, instead of -Infinity