Skip to content

Instantly share code, notes, and snippets.

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

  • Save basekays/5b3f36cbca23e5f906e47915a065d397 to your computer and use it in GitHub Desktop.

Select an option

Save basekays/5b3f36cbca23e5f906e47915a065d397 to your computer and use it in GitHub Desktop.
reduce_last.js
_.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;
};
//should pass the second item of the array into the iterator first if a memo is not passed in
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment