Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

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


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *