0

I had a question in an interview:

how to implement add(1)(2)(3).....();

Below is the case for 3 instances. But how to stretch it for more instances? The stop condition for the recursion is the function with an empty argument (IIFE)

function add(a){
   return function add(b){
     return function add(c){
       return a+ b+ c;
     }
   }
}
shura
  • 1
  • 3
  • See [Javascript - Create a currying function returning the list of parameters](/a/60251875/4642212). The condition would just be `arg.length`. Another close match would be [Variadic curried sum function](/a/35688303/4642212). – Sebastian Simon Nov 24 '21 at 11:28
  • The concepts you're looking for are called "currying" and "partial application". A bit of research into those ideas should help you along. – Jamie Dixon Nov 24 '21 at 11:29
  • A particularly simple version is at https://stackoverflow.com/a/60519986. But you should note that an [`IIFE`](https://developer.mozilla.org/en-US/docs/Glossary/IIFE) is more than just the notion of calling a function with an empty parameter list. – Scott Sauyet Nov 24 '21 at 16:01

0 Answers0