logo
logo

Find Maximum Number using reduce()

Find Maximum Number using reduce()

Write a function
findMaxNumber
that takes an array of numbers as input, and returns the maximum number using the
reduce()
method.
Console
Submit
Solution
00:00

Solution Walkthrough for Find Maximum Number using reduce()

This implementation first checks if the input array is empty. If it is, we return
undefined
, as there is no maximum number to be found in an empty array.
Next, we use
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.