Skip to content

Instantly share code, notes, and snippets.

@forestrf
Last active August 29, 2015 14:13
Show Gist options
  • Save forestrf/a2fb23484044e79ebd13 to your computer and use it in GitHub Desktop.
Save forestrf/a2fb23484044e79ebd13 to your computer and use it in GitHub Desktop.
add(1)()(3)(8).valueOf();
function add() {
var slice = Array.prototype.slice, t = 0;
function add2(k) {
k = slice.call(arguments);
for(var j in k)
t += k[j];
return add2;
}
add2.valueOf = function(){return t};
return add2.apply(null, slice.call(arguments));
}
function add(){function a(b){b=d.call(arguments);for(var e in b)c+=b[e];return a}var d=Array.prototype.slice,c=0;a.valueOf=function(){return c};return a.apply(null,d.call(arguments))};
console.log(+add(1,2)(3,4)(5,6,7)()(8)); //36
console.log(+add(3)(5)); //8
console.log(+add(4)()(6)); //10
console.log(+add(8)()(9)); //17
// http://www.quora.com/Can-anyone-write-a-Javascript-function-that-does-this-add-1-2-8-should-return-11-i-e-the-sum-of-the-arguments-given-number-of-arguments-can-be-any-natural-number
// https://www.linkedin.com/groups/Can-anyone-write-Javascript-function-121615.S.5962319091699113985
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment