Last active
October 14, 2021 18:31
-
-
Save tjinlag/aba8a3eebe68102d8ec5730451eab143 to your computer and use it in GitHub Desktop.
The implementation of reduceRight function in JavaScript by for loop
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.reduceRightFunc = function(callbackFn, initialValue) { | |
let accumulator = initialValue, index = this.length - 1; | |
if (initialValue === undefined) { | |
if (!this.length) { | |
throw new TypeError("Reduce of empty array with no initial value"); | |
} else { | |
accumulator = this[this.length - 1]; | |
index = this.length - 2; | |
} | |
} | |
for (; index >= 0; index--) { | |
accumulator = callbackFn(accumulator, this[index], index, this); | |
} | |
return accumulator; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment