# Arguments Optional: Recursive function takes 2nd input

Hi! I found this solution to the Arguments Optional intermediate javascript challenge. Can someone please explain to me what’s happening in the first if statement? How does this recursive function actually take a second input? I don’t understand why the second input is passed to the anonymous function as “x” while “a” is already coded into the return statement…

``````  **Your code so far**
``````
``````
var len = arguments.length;
var a = arguments[0];
var b = arguments[1];
var isNum = function(arg) { return Number.isFinite(arg); };
if (len === 1 && isNum(a)) {
return function(x) {
if (isNum(x)) {
return a + x;
}
};
}
else if (len === 2 && isNum(a) && isNum(b)) {
return a + b;
}
}

``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36`.

Challenge: Arguments Optional

There is no recursion.

The sort of funny thing going on is that a function is an object in JavaScript, and any object can be saved to a variable or returned from a function.

In this case, a now function is returned, and this function takes an argument, x, and return the sum of x and a if x is a number.

For what it’s worth, the variable names a, b, and x are not great.

``````function addTogether() {
let len = arguments.length;
let first = arguments[0];
let second = arguments[1];

// Argument checking helper
function isNum(arg) {
return Number.isFinite(arg);
};

if (len === 1 && isNum(first)) {
// Create new function
if (isNum(value)) {
return first + value;
}
};
// return this function
}
else if (len === 2 && isNum(first) && isNum(second)) {
return first + second;
}
}
``````

I have rewritten this with slightly different syntax.

This function never calls itself. This function creates and returns a new function.

1 Like

2 Likes

Thank you for explaining! I see now that it isn’t recursion. However I’m still stuck on the crux of this solution. What is actually passed to addThis? From my understanding, the second input passes to the function and then is added to “a” within the function. But why is “a” coded into the function while the second input is passed as (value)? The challenge hint says not to worry about prompting the second input, and maybe that’s why I feel like there’s a gap in my understanding.

Nothing is passed to addThis inside of this function call to addTogether (but every function does have a ‘closure’, which I’ll explain below)

In JavaScript, you can create a function inside of a function. You can return a function from a function for later use.

So in this case, we could

``````const addThree = addTogether(3);

// some time later