Skip to content

Instantly share code, notes, and snippets.

@gladchinda
Last active January 11, 2018 19:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gladchinda/8fd6ee95e0a0ef33f3f4b3a0977427b2 to your computer and use it in GitHub Desktop.
Save gladchinda/8fd6ee95e0a0ef33f3f4b3a0977427b2 to your computer and use it in GitHub Desktop.
Solution to sum function challenge. Here are some examples: ( add(2) == 2 ) ( add(1)(2)(3) == 6 )
var add = function add() {
var args = Array.prototype.slice.call(arguments);
var fn = Function.prototype.bind.apply(add, [null].concat(args));
fn.valueOf = function() {
return args.reduce(function(a, b) {
return a + parseInt(b);
}, 0);
};
return fn;
}
const add = function add(...args) {
const fn = Function.prototype.bind.apply(add, [null, ...args]);
fn.valueOf = () => args.reduce((a, b) => (a + parseInt(b)), 0);
return fn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment