Goal: Create a function that sums arguments from a chain of calls using the currying technique.
You need to implement a sum function that allows you to add numbers passed to it sequentially through a chain of calls. The special feature of the function is that it should return the accumulated sum only when it is called without arguments.
This technique is known as currying (or partial application) and is a powerful tool in functional programming in JavaScript.
// Examples of how the function works:
sum(); // Returns 0
sum(5)(); // Returns 5
sum(5)(10)(); // Returns 15
sum(5)(10)(15)(); // Returns 30sum function should take the first argument and return another (inner) function.sum is called without arguments (sum()), it should immediately return 0.const sum = (initialValue) => {
// If sum() is called without arguments, return 0.
if (initialValue === undefined) {
return 0;
}
// Store the current sum in a closure.
let total = initialValue;
// Return an inner function that will handle
// subsequent calls in the chain.
const innerFunc = (nextValue) => {
// If the inner function is called without arguments (e.g., ...()),
// it's a signal to finish. Return the accumulated sum.
if (nextValue === undefined) {
return total;
}
// If an argument is passed, add it to the sum
// and return the function itself to continue the chain.
total += nextValue;
return innerFunc;
};
return innerFunc;
};Step-by-step code explanation:
const sum = (initialValue) => { ... }
sum is the outer function that starts the process. It takes an optional initialValue.initialValue is not passed (sum()), we immediately return 0.let total = initialValue;
total variable to store the accumulated sum. It is “closed over” and will be available to the inner function innerFunc on every subsequent call.const innerFunc = (nextValue) => { ... }
sum and handles all subsequent calls.if (nextValue === undefined): We check if an argument was passed. If not (()), it means the end of the chain, and we return total.total += nextValue;: If there is an argument, we add it to total.return innerFunc;: Most importantly — we return the innerFunc function itself. This allows it to be called again and again, creating a chain.Why it works (the magic of closures):
Each time you call sum(5), a new scope is created with a total = 5 variable, and innerFunc is returned, which “remembers” this total. When you call (10), innerFunc updates total to 15 and returns itself again. This process continues until you call (), which terminates the chain.
// Call without arguments
sum(); // 0
// One argument
sum(13)(); // 13
// Two arguments
sum(5)(5)(); // 10
// Multiple arguments
sum(10)(7)(41)(); // 58
// Working with negative numbers
sum(10)(-5)(); // 5sum.sum() without arguments must return 0.sum(a)(b)...(n)() must return the sum of all passed numbers.Goal: Create a function that sums arguments from a chain of calls using the currying technique.
You need to implement a sum function that allows you to add numbers passed to it sequentially through a chain of calls. The special feature of the function is that it should return the accumulated sum only when it is called without arguments.
This technique is known as currying (or partial application) and is a powerful tool in functional programming in JavaScript.
// Examples of how the function works:
sum(); // Returns 0
sum(5)(); // Returns 5
sum(5)(10)(); // Returns 15
sum(5)(10)(15)(); // Returns 30sum function should take the first argument and return another (inner) function.sum is called without arguments (sum()), it should immediately return 0.const sum = (initialValue) => {
// If sum() is called without arguments, return 0.
if (initialValue === undefined) {
return 0;
}
// Store the current sum in a closure.
let total = initialValue;
// Return an inner function that will handle
// subsequent calls in the chain.
const innerFunc = (nextValue) => {
// If the inner function is called without arguments (e.g., ...()),
// it's a signal to finish. Return the accumulated sum.
if (nextValue === undefined) {
return total;
}
// If an argument is passed, add it to the sum
// and return the function itself to continue the chain.
total += nextValue;
return innerFunc;
};
return innerFunc;
};Step-by-step code explanation:
const sum = (initialValue) => { ... }
sum is the outer function that starts the process. It takes an optional initialValue.initialValue is not passed (sum()), we immediately return 0.let total = initialValue;
total variable to store the accumulated sum. It is “closed over” and will be available to the inner function innerFunc on every subsequent call.const innerFunc = (nextValue) => { ... }
sum and handles all subsequent calls.if (nextValue === undefined): We check if an argument was passed. If not (()), it means the end of the chain, and we return total.total += nextValue;: If there is an argument, we add it to total.return innerFunc;: Most importantly — we return the innerFunc function itself. This allows it to be called again and again, creating a chain.Why it works (the magic of closures):
Each time you call sum(5), a new scope is created with a total = 5 variable, and innerFunc is returned, which “remembers” this total. When you call (10), innerFunc updates total to 15 and returns itself again. This process continues until you call (), which terminates the chain.
// Call without arguments
sum(); // 0
// One argument
sum(13)(); // 13
// Two arguments
sum(5)(5)(); // 10
// Multiple arguments
sum(10)(7)(41)(); // 58
// Working with negative numbers
sum(10)(-5)(); // 5sum.sum() without arguments must return 0.sum(a)(b)...(n)() must return the sum of all passed numbers.The code editor is intentionally hidden on mobile.
Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.
📖 Now: Study the task, think through the solution. Act like a strategist.
💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!