Implement Reduce Polyfill from Scratch | Frontend Problem Solving | JavaScript Interview Question
@Yomesh Gupta

In this question, you need to implement the polyfill for Array.prototype.reduce.

Your implementation (customReduce method) should exist on Array's prototype chain.

From MDN:

The reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.


customReduce(callbackFn, intialValue);


  1. callbackFn

A "reducer" function is called with the following arguments:

  • previousValue

The value resulting from the previous call to callbackFn. On first call, initialValue if specified, otherwise the value of array[0].

  • currentValue

The value of the current element. On first call, the value of array[0] if an initialValue was specified, otherwise the value of array[1].

  • currentIndex

The index position of currentValue in the array. On the first call, 0 if initialValue was specified, otherwise 1.

  • array

The array being traversed.

initialValue (Optional)

A value to which previousValue is initialized the first time the callback is called. If initialValue is specified, that also causes currentValue to be initialized to the first value in the array. If initialValue is not specified, previousValue is initialized to the first value in the array, and currentValue is initialized to the second value in the array.

Return value

The value that results from running the "reducer" callback function to completion over the entire array.


Loading IDE...