Find Maximum Number using reduce()
findMaxNumber
that takes an array of numbers as input, and returns the maximum number using the reduce()
method.Solution Walkthrough for Find Maximum Number using reduce()
Spoiler Alert!
Don't read this unless you've already solved the problem.
undefined
, as there is no maximum number to be found in an empty array.reduce()
to iterate over the array and find the maximum number. The initial value of the accumulator is set to the first element of the array arr[0]
. We then compare each element of the array to the current maximum using Math.max()
. If the current element is greater than the current maximum, we return the current element, otherwise we return the current maximum.