Currying a function that takes infinite arguments
<p>Using ES5, how do you curry a function that takes infinite arguments.</p>

<pre class="lang-js prettyprint-override"><code>function add(a, b, c) {
    return a + b + c;
}
</code></pre>

<p>The function above takes only three arguments but we want our curried version to be able to take infinite arguments.</p>

<p>Hence, of all the following test cases should pass:</p>

<pre class="lang-js prettyprint-override"><code>var test = add(1);

test(2);     //should return 3
test(2,3);   //should return 6
test(4,5,6); //should return 16
</code></pre>

<p>Here is the solution that I came up with:</p>

<pre class="lang-js prettyprint-override"><code>function add(a, b, c) {
    var args = Array.prototype.slice.call(arguments);

    return function () {
        var secondArgs = Array.prototype.slice.call(arguments);
        var totalArguments = secondArgs.concat(args);

        var sum = 0;

        for (i = 0; i &lt; totalArguments.length; i++) {
            sum += totalArguments[0];
        }

        return sum;
    }
}
</code></pre>

<p>However, I have been told that it's not very &ldquo;functional&rdquo; in style.</p>