Skip to content

Instantly share code, notes, and snippets.

@sketchbuch
Last active January 25, 2020 07:24
Show Gist options
  • Save sketchbuch/9334637b750f456b7068a07dcb026c79 to your computer and use it in GitHub Desktop.
Save sketchbuch/9334637b750f456b7068a07dcb026c79 to your computer and use it in GitHub Desktop.
JS - My own version of array.reduce - based on a video by MPJ (FunFunFunction)
const numbers = [1, 2, 3]
const result = numbers.reduce((acc, cur) => acc += cur, 0)
result //?
const summr = (total, addition) => total + addition
const myReducer = (reducerFunc, initialValue, arr) => {
let accumilation = initialValue
for (let i = 0; i < arr.length; i++) {
accumilation = reducerFunc(accumilation, arr[i])
}
return accumilation
}
const test = myReducer(summr, 0, numbers)
test //?
const summrTest = summr(10, 2) //?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment