Created
January 13, 2016 00:23
-
-
Save aray12/455e4dd8eddfbb0e5ed2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const someArray = [4,3,3,2,5,6,63,1,43,34,46,46,2,35,35,64,75]; | |
function reduction(list, callback) { | |
if (typeof callback !== 'function') { | |
throw new Error('callback not function'); | |
} | |
let count = 1; | |
function reducting(current) { | |
if (++count > list.length - 1) { | |
return current; | |
} | |
return reducting(callback(current, list[count], count, list)); | |
} | |
return reducting(callback(list[0], list[1])); | |
} | |
const result = reduction(someArray, (x, y) => x + y); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment