Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: timoxley/functional-javascript-workshop
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: basemath/functional-javascript-workshop
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Apr 15, 2017

  1. Copy the full SHA
    28ec841 View commit details
Showing with 3 additions and 4 deletions.
  1. +3 −4 exercises/basic_recursion/solution/solution.js
7 changes: 3 additions & 4 deletions exercises/basic_recursion/solution/solution.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
function reduce(arr, fn, initial) {
return (function reduceOne(index, value) {
if (index > arr.length - 1) return value // end condition
return reduceOne(index + 1, fn(value, arr[index], index, arr)) // calculate & pass values to next step
})(0, initial) // IIFE. kick off recursion with initial values
if (arr.length === 0) return initial // end condition
var subset = arr.slice(0, arr.length - 1) // setup recursive call with left subset
return fn(reduce(subset, fn, initial), arr[arr.length - 1]) // pass last element to traverse ltr
}

module.exports = reduce