title: Javascript: How to return sum of function with multiple sets of inputs
body: <p>I have this function that need to have the ability to take in multiple sets of inputs and return the sum of every input. For example: </p>
  
  <pre><code>magicFunction(1)(2)(3) =&gt; 6
  </code></pre>
  
  <p>I accomplished this with the code below. 'f(y)' will run until there are no input sets left.</p>
  
  <pre><code>function magicFunction(x) {
    var sum = x;  
  
    function f(y) { 
      sum += y;
      return f;
    };  
    return f;
  }
  </code></pre>
  
  <p>My issue is I want the function to return the sum, but this function will return '[Function: f]' at the end. So currently, the function does this: </p>
  
  <pre><code>magicFunction(1)(2)(3) =&gt; [Function: f]
  </code></pre>
  
  <p>I can't figure out how to return the sum after the last input. Is there a way I can check if the code reached the last input set? So instead of 'return f', I can run 'return sum'.</p>
  
  <p>I thought about adding a 'toString' method and then use 'console.log' to output the result. But i'm looking for the function to return the value, not just output the value onto the console.</p>