DEV Community 👩‍💻👨‍💻

Discussion on: JavaScript Interview: Write a custom reduce function

kennethlum profile image
Kenneth Lum

The current solution right now as of May 20, 2021, is that it uses function customReduce(arr, reducer, initialValue = 0) { which is to assume initialValue is 0, but it can't always be the case.

[1,3,5].reduce((a,b) => a * b)   // => 15
customReduce([1,3,5], (a,b) => a * b)   // => 0
Enter fullscreen mode Exit fullscreen mode

so essentially, one way is that when there is no initial value, we'd use the first value as the initial value instead.

amitkhonde profile image
Amit Khonde Author

Thank you for pointing out Kenneth. Will edit that part definitely.