• 0

Implement reduce in javascript

Reducing an array to a single value

Often we have to iterate over the array by accessing multiple items at a time. For example, if you have to find the largest element in an array, then you have to keep track of the largest value in a closure while iterating over it. This behavior can be abstracted using Array.prototype.reduce().

This post is a follow up post of the
- Getting started Functional Programming in JavaScript. I would recommend reading that post first.

Problem

Find the maximum element in the given array

Let's solve with Array.prototype.forEach()



Implement Array.prototype.reduce() using Array.prototype.forEach().


Further Reading