Skip to content

Instantly share code, notes, and snippets.

@nch3ng
Last active December 30, 2020 23:47
Show Gist options
  • Save nch3ng/bc61deab230ccc9c6dbfa46c0a1dd537 to your computer and use it in GitHub Desktop.
Save nch3ng/bc61deab230ccc9c6dbfa46c0a1dd537 to your computer and use it in GitHub Desktop.
my implementation of the array reduce
Array.prototype.myReduce = function(fn, initialValue) {
let accValue = [];
let currentIndex = 0;
if (this.length === 0)
return initialValue;
accValue[currentIndex] = fn(initialValue, this[0]);
for(let i = 1; i < this.length; i++) {
accValue[i] = fn(accValue[i-1], this[i]);
currentIndex = i;
}
return accValue[currentIndex];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment