Skip to content

Instantly share code, notes, and snippets.

@yourpalsonja
Last active December 20, 2017 20:41
Show Gist options
  • Save yourpalsonja/84554553634e8dfd867ff16552ef3e4e to your computer and use it in GitHub Desktop.
Save yourpalsonja/84554553634e8dfd867ff16552ef3e4e to your computer and use it in GitHub Desktop.
Recursive Reduce
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