Last active
December 30, 2020 23:47
-
-
Save nch3ng/bc61deab230ccc9c6dbfa46c0a1dd537 to your computer and use it in GitHub Desktop.
my implementation of the array reduce
This file contains 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.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