-5

I need a single function which will return the below output.

add(1)     =  1
add(1)(2)  =  3
add(1)(2)(3) = 6

like wise the function should return the result.

I want a function function add(){} to perform the above task.

Please help me with an optimized solution

Raja Sekar
  • 2,062
  • 16
  • 23

4 Answers4

4

Thanks to @ermouth for initial code.

var add = (function() {
    var factory = function(value) {
        var fn = function(num) {
            return factory(value + num);
        };
        // This is the main hack: 
        // We will return a function that when compared / concatted will call .toString and return a number.
        // Never use this in production code...
        fn.toString = function() {
            return value;
        };
        return fn;
    };
    return factory(0);
})();

add(1); // 1
add(1); // 1
add(1)(2); // 3
add(1)(2)(3); // 6

But beware of equal comparation:

add(2) == add(2) // false
add(2) > add(1) // true
add(2) < add(1) // false
Community
  • 1
  • 1
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
0

You should wrap your function in a closure and redefine function’s toString method.

var add = (function(initNum) {
    var current = initNum || 0;
    var fn = function(num) {
        current += num;
        return fn;
    };
    fn.toString = function() {
        return current
    };
    return fn;
})(0);

add(1)1, then add(2)(3)(4)10

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
ermouth
  • 835
  • 7
  • 12
  • 1
    This will not work: `add(1) // 1` and again `add(1) // 2` ... – Andreas Louv Feb 11 '15 at 14:52
  • @null, oh, I missed that it must restart ( Sorry. – ermouth Feb 11 '15 at 14:58
  • And what does `2+3+4` equal? 10 or ... 9? ;) – Andreas Louv Feb 11 '15 at 15:05
  • @null Surely **1+** 2+3+4 is 10. I mean I misread it must be re-entrant. But question is really brilliant – so please, tell why exactly did you mark it as ‘Too broad’? I’m new at Stackoverflow and do not understand this particular application of the policy. Sorry for off-topic. – ermouth Feb 11 '15 at 16:36
-3

try this:

function add() {
   var erg = 0;
   for(var i in arguments)
       erg += arguments[i];
   return erg; 
}

you can call it like add(1,2,3);

Cracker0dks
  • 2,422
  • 1
  • 24
  • 39
-3

Either you were given a trick question, or you've misunderstood the problem.

You cannot write a function that does this in pure JavaScript (or, for that matter, almost any other language). The problem is that it has to return a number the last time it's called, and a function at all other times. This would be doable if you knew in advance how many times your function is going to be called, but if I understand your question correctly, then you don't know that in advance. You'd need to be able to foretell the future to know when the function would be called for the last time.

You could also do this in a language with macros that let you inspect the syntax of your code as it's being run. This would let you know how long the current chain of calls is, so you'd know when to return a number instead of a function. But JavaScript doesn't do this; in fact, most languages don't.

Is it possible that your interviewers were looking for an add() function that takes a variable number of arguments? That's much easier:

function add() {
    return Array.prototype.reduce.call(
        arguments,
        function (sum, next) {
            return sum + next;
        },
        0
    );
}
The Spooniest
  • 2,863
  • 14
  • 14
  • seems to not working. – Raja Sekar Feb 11 '15 at 14:55
  • You are definitely wrong – we can redefine `.toString` method of the function (yes, function is object). See my answer. – ermouth Feb 11 '15 at 14:57
  • @JERmouth: Your method gets the job done within the specific context of an interview question, but the side effects of overriding `Function.prototype.toString` are likely to cause problems if you try to integrate it into things like module systems or OO libraries. I'd argue that any interviewer devious enough to ask a question like this is also likely to talk about these kinds of problems. – The Spooniest Feb 11 '15 at 15:04
  • Yes, it's a hack, very rare. But what kind of problems exactly you think may happen? – ermouth Feb 11 '15 at 15:13