Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple reduce function
// array to reduce
// combine is a function that will do something to the current value and the current element
// current is either the start provided or 0 if nothing is provided?
function reduce(array, combine, start) {
var current = start || 0;
for (var i = 0; i < array.length; i++)
current = combine(current, array[i]);
return current;
}
console.log(reduce([1, 2, 3, 4], function(a, b) {
return a + b;
}, 0));
// → 10
@CraigRodrigues

This comment has been minimized.

Copy link
Owner Author

@CraigRodrigues CraigRodrigues commented Nov 20, 2016

From Eloquent JS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment