Created
April 3, 2019 09:20
-
-
Save johnnyBira/ab934024f7e555e40db5459fea285e4a to your computer and use it in GitHub Desktop.
Reduce algo
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
function reduce(arr, fn, initial) { | |
let result; | |
function recursive(i = 0, acc = initial) { | |
if (i < arr.length) { | |
result = fn(acc, arr[i], i) | |
recursive(i + 1, result) | |
} | |
return result | |
} | |
recursive(); | |
return result; | |
} | |
var test = reduce([1,2,3], (acc, item, i) => { | |
console.log(acc) | |
return([...acc, item]) | |
}, []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment