Vinayak Sakhare
Vinayak Sakhare

Reputation: 798

closure in javascript with multiple empty calls to sum function

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.

Upvotes: 0

Views: 1755

Answers (6)

Daniel
Daniel

Reputation: 18682

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.

Upvotes: 4

David
David

Reputation: 1159

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;
        }

    }
}

Upvotes: 0

Sourabh Somani
Sourabh Somani

Reputation: 2138

    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));

Upvotes: 0

Arnav Aggarwal
Arnav Aggarwal

Reputation: 789

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

Upvotes: 0

Sean
Sean

Reputation: 337

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/

Upvotes: 0

CR Drost
CR Drost

Reputation: 9817

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;
}

Upvotes: -1

Related Questions