Skip to content

Instantly share code, notes, and snippets.

@tokdaniel
Created November 3, 2020 11:00
Show Gist options
  • Save tokdaniel/d5a23d736b7a0a0ba0cc390f03ab7d12 to your computer and use it in GitHub Desktop.
Save tokdaniel/d5a23d736b7a0a0ba0cc390f03ab7d12 to your computer and use it in GitHub Desktop.
// 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