A friend asked a task how to do the following:
sum (q, e) = sum (q)(e); UPDATE function must take an unlimited number of arguments.
A friend asked a task how to do the following:
sum (q, e) = sum (q)(e); UPDATE function must take an unlimited number of arguments.
To accept the list of parameters, it is enough to add the ability to process them, for example, using the reduce function.
function sum() { function add(a, b) { return a + b; }; var num = [].reduce.call(arguments, add); function inner() { return sum.apply(this, [].concat.apply([num], arguments)); } inner.toString = inner.valueOf = inner.toJSON = function() { return num; } return inner; } function o(prepend, val){ document.getElementById('res').innerHTML += (prepend? (prepend+": "):"")+(typeof val == 'string'? val : JSON.stringify(val)) + '<br />'; } o('sum(1,2,3)(4,5,6)(7)',sum(1,2,3)(4,5,6)(7)); o('sum(1,2,3)',sum(1,2,3)); o('sum(1)(2,3)', sum(1)(2,3)); o('sum(1,2)(3)',sum(1,2)(3)); o('sum(1,2)(3)+"4"',sum(1,2)(3)+"4"); o('sum(1)(2,3)+4',sum(1)(2,3)+4); //10 <div id="res"></div> The question is not quite clear, but perhaps it means something like that?
var sum = function(q) { return function(e) { return q + e; } } Source: https://ru.stackoverflow.com/questions/527369/
All Articles