Sobes.tech
Junior — Senior

Cascade adder through call chain

livecode

Task condition

It is necessary to implement a summator function that allows passing numbers sequentially through a chain of calls. Each call takes one value and returns a function ready to accept the next. When the call is made without an argument, the function outputs the accumulated sum to the console.

Example of how it works:

// Example of a summator:
f(a)(b)(c)(d)   // -> a + b + c + d
f(a)(b)         // -> a + b
f(a)(b)()       // -> console.log(a + b)
f(a)(b)(c)(d)   // -> a + b + c + d

f(a)(b)(c)(d)

function f(x) {
  return (y) => 
    y ? f(x + y) : console.log(x);
}