Skip to content

Instantly share code, notes, and snippets.

@basekays
Created January 25, 2017 00:19
Show Gist options
  • Select an option

  • Save basekays/0685d2b25730766e8801e9b950598f98 to your computer and use it in GitHub Desktop.

Select an option

Save basekays/0685d2b25730766e8801e9b950598f98 to your computer and use it in GitHub Desktop.
/*
_.reduce = function(collection, iterator, accumulator) {
for (var i = 0; i < collection.length; i++) {
accumulator = iterator(accumulator, collection[i]);
}
if (accumulator === undefined) {
return collection[0];
}
return accumulator;
};
*/
_.reduce = function(collection, iterator, accumulator) {
if (accumulator === undefined) {
accumulator === collection[0];
} else {
for (var i = 0; i < collection.length; i++) {
accumulator = iterator(accumulator, collection[i]);
}
}
return accumulator;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment