Last active
December 20, 2017 20:41
-
-
Save yourpalsonja/84554553634e8dfd867ff16552ef3e4e to your computer and use it in GitHub Desktop.
Recursive Reduce
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
var boringArray = [1,2,3,4]; | |
var datesArray = [1878, 1980, 1535, 1879]; | |
var reducer = (a, b) => a + b; | |
var earliestReducer = (a, b) => { | |
if(a < b) { | |
return a; | |
} | |
return b; | |
} | |
const rReduce = (arr, func, acc) => { | |
acc ? acc : arr[0]; | |
if(arr.length === 0) { | |
return acc; | |
} | |
return rReduce(arr.slice(1), func, func(acc, arr[0])) | |
}; | |
console.log(rReduce(datesArray, earliestReducer)); | |
console.log(rReduce(boringArray, reducer, 0)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment