0

I faced this question in one interview. I did not get how to solve this. Question: Write a sum function which will add 2 numbers, but numbers can be passed to a function in following ways:

  1. sum(3)(4) // answer should be 7
  2. sum(3)()(4)//answer should be 7
  3. sum(3)()()()()(4) //answer should b 7

I can solve first function using closure, in fact for the second function also I can check the arguments and if arguments length is zero I can again make a call to sum to except next parameter. But how to make it generic ? Means even your first parameter and last parameter has 'N' number of calls & those can be empty or parameterized, it should return sum.

1
  • I've added a video with detailed explanation and solution. Commented Jul 24, 2017 at 14:36

6 Answers 6

4

Recorded a video how to solve it:

https://youtu.be/7hnYMIOVEg0

Text answer:

function sum(numberOne) {
    return function innerSum(numberTwo) {
        if (typeof(numberTwo) === 'number') {
            return numberOne + numberTwo;
        }

        return innerSum;
    }
}

Output:

sum(3)(4); => 7
sum(5)()()(10); => 15

Basically, you need to return inner function (innerSum) up until you receive a value - then you return number.

You could also choose another name - like _sum(), or addToFirstNumber() for your method.

Sign up to request clarification or add additional context in comments.

Comments

0

You can always return a function from within a function:

let a;

function sum(value) {
  if (typeof value !== "number") {
    return sum;
  }

  if (typeof a !== "number") {
    a = value;
    return sum;
  }

  let result = a + value;
  a = null;
  return result;
}

see https://jsfiddle.net/d9tLh11k/1/

Comments

0

function sum(num1) {
  return function sum2(num2) {
    if(num2 === undefined) {
      return sum2;
    }

    return num1 + num2;
  }
}

console.log(sum(4)()()()(3)); // -> 7

Or in ES6:

const add = num1 => num2 => num2 === undefined ? add(num1) : num1 + num2;

console.log(add(4)()()()()()(3)); // -> 7

Comments

0

    function add (n) {
    var func = function (x) {
        if(typeof x==="undefined"){
           x=0;
        }
        return add (n + x);
    };
    
    func.valueOf = func.toString = function () {
        return n;
    };
    
    return func;
    }
    console.log(+add(1)(2)(3)()()(6));

I have already given answer of this question Here

but according to your question I have modified that

function add (n) {
var func = function (x) {
    if(typeof x==="undefined"){
       x=0;
    }
    return add (n + x);
};

func.valueOf = func.toString = function () {
    return n;
};

return func;
}
console.log(+add(1)(2)(3)()()(6));

Run code like that

console.log(+add(1)(2)(3)()()(6));

7 Comments

Accept this answer if you got your answer Because it will be helpful for others as well
This seems to be more generic as it can take more than 2 parameters.
Yes, This is generic because it can take n number of parameters. Cheers :)
I doubt it solves the problem if you need to put + before it. There was no mention of that in the question.
I don't buy it. :D There was no mention of casting functions before logging value in question. But, still, good interesting solution. :)
|
0

This should do it

function sum(num1) {
    if (arguments.length === 0){
        return sum;
    }

    return function innerSum(num2) {
        if (arguments.length > 0){
            return num1 + num2;
        }else{
            return innerSum;
        }

    }
}

Comments

-1

You can do this in a number of ways, but mostly you'll want named recursion. That is, you can have something like:

sum = start => (...args) => args.length? args[0] + start : sum(start)

but it might look cleaner to write this as:

function sum(start) {
   function out(...args) {
       return args.length? start + args[0] : out
   }
   return out;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.