Last active
August 29, 2015 14:13
-
-
Save forestrf/a2fb23484044e79ebd13 to your computer and use it in GitHub Desktop.
add(1)()(3)(8).valueOf();
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
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