DEV Community

loading...

Discussion on: JavaScript Interview: Write a custom reduce function

Collapse
bugb profile image
bugb • Edited

A simple reduceRight using recursion implementation would be like this:

customReduce = (arr, reducer, initialValue = null) =>
  arr.length > 1 ?
  customReduce(arr.slice(1), reducer, reducer(arr[0], initialValue)) :
  reducer(arr[0], initialValue)
Enter fullscreen mode Exit fullscreen mode