I have this function that need to have the ability to take in multiple sets of inputs and return the sum of every input. For example:
magicFunction(1)(2)(3) => 6
I accomplished this with the code below. 'f(y)' will run until there are no input sets left.
function magicFunction(x) {
var sum = x;
function f(y) {
sum += y;
return f;
};
return f;
}
My issue is I want the function to return the sum, but this function will return '[Function: f]' at the end. So currently, the function does this:
magicFunction(1)(2)(3) => [Function: f]
I can't figure out how to return the sum after the last input. Is there a way I can check if the code reached the last input set? So instead of 'return f', I can run 'return sum'.
I thought about adding a 'toString' method and then use 'console.log' to output the result. But i'm looking for the function to return the value, not just output the value onto the console.