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.

  • Try to write more detailed questions. To get an answer, explain exactly what you see the problem, how to reproduce it, what you want to get as a result, etc. Give a sample code. - Grundy

2 answers 2

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> 

  • This is hardcore: D - Vasily Barbashev
  • @ Vasily Barbashev, and on en.SO this is a minus one for me :) - Grundy

The question is not quite clear, but perhaps it means something like that?

 var sum = function(q) { return function(e) { return q + e; } } 
  • Well, of course, this option can also be, but I'm sorry, I forgot to clarify that the function should take an unlimited number of arguments, for example, as described here habrahabr.ru/post/226325 - Goshka Tarasov
  • 3
    @ GoshkaTarasov, then what is not satisfied with the option for the link? - Grundy