Created
November 20, 2016 20:06
-
-
Save CraigRodrigues/e9f204c30925547662d233e7ea9fee55 to your computer and use it in GitHub Desktop.
Simple reduce function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From Eloquent JS