Assuming only one argument is passed in, it will enter the second if statement. But second is still undefined, so we return the function addTogether with second passed in as undefined enetering the second if statement and so on forever.
function addTogether() {
const [first, second] = arguments;
if (typeof(first) !== "number")
return undefined;
if (second === undefined)
return (second) => addTogether(first, second);
if (typeof(second) !== "number")
return undefined;
return first + second;
}
This isn’t passing second into the function. In this case, second is the name of the argument for the function. When you define a function with parameters you give the parameters names so you can use their values in the function body:
function myFunc(argA) { ... }
argA assumes the value of whatever we provide when we call the function:
myFunc(10);
Inside the function body argA will be 10. This is the same thing that is happening when you return a function. You are just using the same name for the parameter as you are for one of the original arguments, but they are not the same thing.
If this is still confusing then use a different name for the parameter in the function you are returning.
Note that this is basically the same as the other provided solution but this one returns an anonymous function while the other creates the function before returning it.
function addTogether() {
const [first, second] = arguments;
// First argument is not a number
if (typeof(first) !== "number") {
return undefined;
}
// First argument is a number
// and second argument is not defined
else if (second === undefined) {
function addSecond(second) {
// New argument is not a number
if (typeof(second) !== "number") {
return undefined;
}
// New argument is a number
else {
return first + second;
}
}
// Note: returning a *function*
return addSecond;
}
// First argument is a number
// and second argument is not a number
else if (typeof(second) !== "number") {
return undefined;
}
// First argument is a number
// and second argument is a number
else {
return first + second;
}
}
it enters the second if statement, and defines addSecond function and when the definition is complete it is called. But it still feels like it should be return addSecond(7);
No, it is returned. The first else if statement returns a function.
How would you know that the argument is going to be 7? You are returning a function that takes one argument. You don’t know the value of that argument until it is called.
This is a two step process:
Call addTogether with one number. This returns a function that takes one number and then adds it to the number passed into addTogether.
Call the returned function, passing in the second number.
Do you see how you don’t know what the second number is going to be until the function returned by addTogether is actually called and a number is passed into it?
Which is the instructions. If one argument is provided and that argument is a number, then you need to return a function. This function can later be called after it has been returned.
This is calling the addSecond function. Look again a few posts above where I split this into two parts. Granted, we are never actually using the name addSecond to call it, but that name is just a convenience for you, you don’t have to define the returned function with a name. For example, in the original code you posted you were returning an anonymous function.