Middle+
<script> function sum(valueA) { throw 'Not implemented' } console.log(sum(1)()); // 1 console.log(sum(1)(2)()); // 3 console.log(sum(1)(2)(-3)()); // 0 </script>
sobes.tech AI
Answer from AI
The sum function should support chaining calls with arguments and end with a call without arguments that returns the sum of all passed numbers. This can be achieved using a closure that accumulates the sum and returns a function that either accepts the next argument or, if called without arguments, returns the result.
Example implementation in JavaScript:
function sum(valueA) {
let total = valueA;
function inner(valueB) {
if (arguments.length === 0) {
return total;
}
total += valueB;
return inner;
}
return inner;
}
console.log(sum(1)()); // 1
console.log(sum(1)(2)()); // 3
console.log(sum(1)(2)(-3)()); // 0