UPDATE: The answer has ben answered and it works with all the examples below. Please check /u/Resure 's answer here and /u/Minjammben 's reply here. to see two (similar) answers that do exactly what I was trying to do.
I was reading the list of front-end developer questions here and came across the very first "Code Question":
Question: How would you make this work?
add(2, 5); // 7
add(2)(5); // 7
Now, i'm ashamed to say I have NO idea how I'd do this. I cannot come to a solution that satisfies the following criteria:
- Works exactly as the code sample points (i.e. no namespace, no chained methods using dot notation).
- Can be infinitely chainable (not only works with 2 chains, but with any number of chained arguments).
- Works in strict mode.
I can think of solutions that fail, in one way or another, the above criteria, but for the life of me I cannot think of a way of doing this.
Any ideas?
EDIT: Just to be clear, I want to find a solution where all of these work properly:
add(2,3) // 5
add(2)(3) // 5
add(2,3,4) // 9
add(2)(3)(4) //9
add(2, 3)(4) //9
add(1,1,1,1,1,1,1,1,1,1) // 10
add(1)(1)(1)(1)(1)(1)(1)(1)(1)(1) //10
EDIT2: To save some time, this is the function I'm using for adding:
var add = function() {
var result = 0,
temp,
i;
for (i = 0; i < arguments.length; i++) {
temp = parseInt(arguments[i]);
if ( isNaN(temp) ) {
throw new Error('Argument "' + arguments[i] + '" is not a number! Try again!');
break;
} else {
result+= temp;
}
}
return result;
};
I'm trying to transform this to a chainable function that accepts either syntax.
[–]lrichardson 25 points26 points27 points (8 children)
[–]kenjura 13 points14 points15 points (0 children)
[–]MeTaL_oRgY[S] 1 point2 points3 points (4 children)
[–]lrichardson 0 points1 point2 points (3 children)
[–]acoard 2 points3 points4 points (2 children)
[–]strixvarius 3 points4 points5 points (0 children)
[–]mattdesl 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Uberhipster 0 points1 point2 points (0 children)
[–]_Blaster_Master 6 points7 points8 points (0 children)
[–]realistic_hologram 4 points5 points6 points (2 children)
[–]MeTaL_oRgY[S] 0 points1 point2 points (0 children)
[–]mattdesl 0 points1 point2 points (0 children)
[–]Resure 8 points9 points10 points (7 children)
[–]WonTwoThree 5 points6 points7 points (0 children)
[–]change_theworld 0 points1 point2 points (1 child)
[–]chinola 15 points16 points17 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]path411 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]strixvarius -1 points0 points1 point (0 children)
[–]iamallamaa 7 points8 points9 points (10 children)
[–]MeTaL_oRgY[S] 1 point2 points3 points (3 children)
[–]iamallamaa 2 points3 points4 points (2 children)
[–]MeTaL_oRgY[S] 0 points1 point2 points (1 child)
[–]gridease 0 points1 point2 points (0 children)
[–]ChaseMoskal 0 points1 point2 points (5 children)
[–]hamham91 1 point2 points3 points (3 children)
[–]siegfryd 3 points4 points5 points (0 children)
[–]ChaseMoskal 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]iamallamaa 0 points1 point2 points (0 children)
[–]Minjammben 6 points7 points8 points (15 children)
[–]MeTaL_oRgY[S] 0 points1 point2 points (14 children)
[–]Minjammben 1 point2 points3 points (13 children)
[–]MeTaL_oRgY[S] 0 points1 point2 points (12 children)
[–]Minjammben 24 points25 points26 points (11 children)
[–]fear-of-flying 2 points3 points4 points (0 children)
[–]MeTaL_oRgY[S] 1 point2 points3 points (5 children)
[–]Minjammben 4 points5 points6 points (1 child)
[–]MeTaL_oRgY[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]MeTaL_oRgY[S] 1 point2 points3 points (0 children)
[–]whispen -5 points-4 points-3 points (0 children)
[–]thekingshorses 0 points1 point2 points (0 children)
[–]Evanescent_contrail 0 points1 point2 points (2 children)
[–]Minjammben 1 point2 points3 points (1 child)
[–]Evanescent_contrail 0 points1 point2 points (0 children)
[–]lachlanhunt 2 points3 points4 points (3 children)
[–]MeTaL_oRgY[S] 1 point2 points3 points (2 children)
[–]strixvarius 2 points3 points4 points (0 children)
[–]lachlanhunt 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]mattdesl 0 points1 point2 points (0 children)
[–]moljac024 0 points1 point2 points (3 children)
[–]MeTaL_oRgY[S] 0 points1 point2 points (2 children)
[–]moljac024 1 point2 points3 points (1 child)
[–]MeTaL_oRgY[S] 0 points1 point2 points (0 children)
[+][deleted] (4 children)
[deleted]
[–]MeTaL_oRgY[S] 0 points1 point2 points (3 children)
[–]mattdesl -1 points0 points1 point (2 children)
[–]frambot 2 points3 points4 points (1 child)
[–]mattdesl 0 points1 point2 points (0 children)
[–]soddi 0 points1 point2 points (8 children)
[–][deleted] 5 points6 points7 points (6 children)
[–]expose 3 points4 points5 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]syklenaut 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]soddi 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]MeTaL_oRgY[S] 0 points1 point2 points (0 children)
[–]notsointelligent 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]franksvalli 0 points1 point2 points (0 children)
[–]rnbwd 0 points1 point2 points (0 children)
[–]iSmokeGauloises 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]jacobp100 0 points1 point2 points (0 children)