Overview
Implement sum(a) that returns a function accepting more numbers. When called with no arguments, returns the total.
Examples
sum(1)(2)(3)(); // 6 sum(5)(); // 5
Solution
Reveal solution
function sum(a) {
return function next(b) {
if (b === undefined) return a;
return sum(a + b);
};
}sum.js
Sum
easycodingJavaScriptFunctions
Overview
Implement sum(a) that returns a function accepting more numbers. When called with no arguments, returns the total.
Examples
sum(1)(2)(3)(); // 6 sum(5)(); // 5
Solution
Reveal solution
function sum(a) {
return function next(b) {
if (b === undefined) return a;
return sum(a + b);
};
}NameTopicDifficulty
103 of 103 problems