Created
November 3, 2020 11:00
-
-
Save tokdaniel/d5a23d736b7a0a0ba0cc390f03ab7d12 to your computer and use it in GitHub Desktop.
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
// Simple implementation of [].reduce | |
Array.prototype.myReduce = function (reducer, initialValue) { | |
let result = initialValue | |
this.forEach(item => { | |
result = reducer(result, item) | |
}) | |
return result | |
} | |
// tests | |
console.log([1 ,2, 3, 4, 5].myReduce((acc, curr) => acc + curr, 0)) | |
console.log([[1,2,3], 4, 5, 6].myReduce((acc, curr) => acc.concat(curr), [])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment