Skip to content

Instantly share code, notes, and snippets.

@roark47
Created August 18, 2020 01:08
Show Gist options
  • Select an option

  • Save roark47/da6b9b45a3abba95b889f13fcae89c93 to your computer and use it in GitHub Desktop.

Select an option

Save roark47/da6b9b45a3abba95b889f13fcae89c93 to your computer and use it in GitHub Desktop.
const reduce = (array, fn, initialValue) => {
let accumlator
if (initialValue != undefined) {
accumlator = initialValue
} else {
accumlator = array[0]
}
if (initialValue === undefined) {
for (let i = 1; i < array.length; i++) {
accumlator = fn(accumlator, array[i])
}
} else {
for (const value of array) accumlator = fn(accumlator, value)
}
return [accumlator]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment