0

I've just met with a very interesting task to solve, but I can't do it myself.

Make a function which do the following input!

console.log(foo(4)(5)(6)); // output: 120

The solution is not this:

function foo(number1,number2,number3){
  return number1 * number2 * number3;
}

In theory it has to use nested functions.

1
  • Are you doing a quiz? I'll give you a hint anyway: the foo function has to return another function, which also returns a function. By the way, nested functions are not considered good practice, maybe you can just answer that! Commented Feb 8, 2017 at 12:13

6 Answers 6

3

It is called currying. You need to return function which it's case returns a function which returns the total. Here we also use a term called closure.

function foo(a){
  return function(b)  {
     return function(c){
       return a * b * c;
     }
   } 
}

console.log(foo(4)(5)(6));

With ES6 syntax

var foo = a => b => c => a*b*c;

console.log(foo(4)(5)(6));

For infinite levels you can do

function foo(value){
  infinite.total = value;
  
  function infinite(val) {
     infinite.total *= val;
     return infinite;
  }
  
  infinite.toString = function() { return infinite.total; }
  
  return infinite;
  
}

console.log(foo(4)(5)(6)(7));

var total = foo(4)(5)(6)(7);
console.log(total);

But this will output the total in that cases, when toString() is called explicitly or implicitly

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

4 Comments

It's called currying !
It was too easy. Very nice! Thx!
Is it possible to do an infinite level of currying? Can I make a function which will work for 'foo(5)' 'foo(5)(6)' and 'foo(1)(2)(3)(4)(5)(6)(7)(8)' without hard coding 8 functions?
@Keatinge see the updated part and also Nina's answer
2

You could use an approach for any length of repeated calls of the function with returning a function and an implementation of toString method.

var foo = function(value) {
    function fn(a) {
        fn.value *= a;
        return fn;
    }
    fn.value = value;
    fn.toString = function () { return fn.value; };
    return fn;
}

console.log(foo(4)(5)(6));
console.log(foo(42));
console.log(foo(42)(44));

1 Comment

It's an interesting approach!
1

try with currying:

function foo(number) {
  return function(number2) {
    return function(number3) {
      return number * number2  number3;
    }
  }
}

console.log(foo(5)(6)(7));

https://jsfiddle.net/mv9bm3tL/1/

More information : https://www.sitepoint.com/currying-in-functional-javascript/

Comments

1
You need nested functions - currying;

console.log(foo(4)(5)(6)); // output: 120


let first = foo(4);
let second = first(5);
let last = second(6);

console.log(last);

function foo(number1,number2,number3){
  return function(number2){
    return function(number3){
      return number1 * number2 * number3;
    }
  }
}

Comments

1

Closures in javascript will help you to achieve this.

function foo(a){
  return function(b){
    return function(c){
      return a*b*c;
    }
  }
}

console.log(foo(4)(5)(6)); //will return 120

Comments

1

Using arrow functions;

var foo = a => b => c => a*b*c;

console.log(foo(4)(5)(6));

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.