Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues
Created November 20, 2016 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CraigRodrigues/e9f204c30925547662d233e7ea9fee55 to your computer and use it in GitHub Desktop.
Save CraigRodrigues/e9f204c30925547662d233e7ea9fee55 to your computer and use it in GitHub Desktop.
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
Copy link
Author

From Eloquent JS

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