Last active
January 11, 2018 19:29
-
-
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 )
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
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; | |
} |
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
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