title: "add" function that works with different combinations of chaining/arguments
body: <p>I'm trying to write an add function that will work in many scenarios.</p>
  
  <pre><code>add(2,2,2) //6
  add(2,2,2,2) //8
  add(2)(2)(2) // 6
  add(2)(2)(2,2).value() //8
  add(2,2)(2) + 2 //8
  add(2).add(2) //4
  add(2,2,2).add(2).add(2,2).value() //12
  add(2,2,2).add(2).value() //8
  </code></pre>
  
  <p>This is what I have so far:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function add(){
      var sum = 0;
      for( var i in arguments ){
          sum += arguments[i];
      }
  
      var ret = add.bind(null, sum);
  
      ret.value = function () {
        return sum;
      }
  
      ret.add = function () {
        for( var i in arguments ){
          sum += arguments[i];
        }
        return sum;
      }
  
      ret.valueOf = function(){ return sum; };
      
      return ret;
  }
  
  console.log(add(2,2,2));
  console.log(add(2,2,2,2));
  console.log(add(2)(2)(2));
  console.log(add(2)(2)(2,2).value());
  console.log(add(2,2)(2) + 2);
  console.log(add(2).add(2));
  console.log(add(2,2,2).add(2).value());
  console.log(add(2,2,2).add(2).add(2,2).value());</code></pre>
  </div>
  </div>
  </p>
  
  <p>I am having a problem with the last two cases:</p>
  
  <pre><code>  add(2,2,2).add(2).add(2,2).value() //12
    add(2,2,2).add(2).value() //8
  </code></pre>
  
  <p>It seems like I would have to keep nesting the add functions if I wanted to chain more than two together and also add the value function to each of them, but obviously I'm missing something simple that will allow me to chain them as much as I like, and call value on any of them.</p>
  
  <p>Also they need to always return ints (not strings), and it seems like sometimes they do and other times they don't?</p>