1

How can we make dynamic multiple bracket functions.

function addThis(x){
   return function(y){
       return x+y;
   }
}

addThis(3)(4) // 7
addThis(3)(4)(5) //12
addThis(3)(4)(5)(6) //18
addThis(1)(1)(1)(1)(1) //5

With single bracket function I could achieve something like that using rest parameter.

 function add(...args) {
     let sum = 0;
     for(let i=0; i<args.length; i++) {
         sum  = sum + args[i];
     }
     return sum;
 }

How can we achieve same with multiple bracket functions.

Santosh
  • 3,477
  • 5
  • 37
  • 75
  • 1
    Why would you want to do that? (getting **exactly** what you asked for is obviously impossible, because if `addThis(3)(4)` is `7`, then `addThis(3)(4)(5)` is `let f=7; f(5)`.) – user202729 Feb 11 '21 at 06:38
  • You might be looking for "builder pattern". – user202729 Feb 11 '21 at 06:39

0 Answers0