Skip to content

Instantly share code, notes, and snippets.

@aray12
Created January 13, 2016 00:23
Show Gist options
  • Save aray12/455e4dd8eddfbb0e5ed2 to your computer and use it in GitHub Desktop.
Save aray12/455e4dd8eddfbb0e5ed2 to your computer and use it in GitHub Desktop.
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