const sum = (...args) => {
        //spread the arguments in storage array
        const storage = [...args];
        
        //base case
        //if invoked without any argument
        if(storage.length === 0){
          return 0;
        }
        //closure
        else{
          //create an inner function
          const temp = function(...args2){
            //get the arguments of inner function
            //merge them in existing storage
            storage.push(...args2);
      
            //if no arguments are passed 
            //return the value
            if(args2.length === 0){
              return storage.reduce((a, b) => a + b, 0);
            }
            //else return the same function again
            else{
              return temp;
            }
          }
      
          //return the function
          return temp;
        }
      }

      Javascript language logo
      Currying in Javascript

      Javascript Interview Cookbook

      When invoked without any arguments, the code will create an inner function that accepts a set of arguments. This inner function will merge the arguments in the storage array. If invoked with arguments, the code will return the value of the inner function.

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.