# Help with Arguments Optional, please

Through some small amount of ingenuity and a bit of outright theft I have been able to cobble together some functions that nearly solve this problem. Unfortunately, I’m also stuck.

I’ve been staring at this code long enough that it’s just turning into a muddy mess in my head.

Any help would be appreciated; I am passing all of the tests except for the second one: “addTogether(2)(3) should return 5”.

Here’s my code:

``````function addTogether() {
//CHECK IF IT BE A NUMBER
function isANum(y){
if (typeof y === "number"){
return true;
}else{
return false;
}
}
//IF THERES 1 ARG TRY TO ADD THE NEXT
if(arguments.length < 2){
if(isANum(arguments[0])){
return function(x){
if (!isANum(x)) {
return undefined;
}else{
return arguments[0] + x;
}
};
}
//IF BOTH ARE NUMBERS, ADD AS NORMAL
}else if(isANum(arguments[0]) && isANum(arguments[1])){
return arguments[0] + arguments[1];

//IF THERES JUST THE ONE ARGUMENT AND IT IS A NUMBER
}else{
return undefined;
}
}

``````

Just to be clear, I’m looking for an explanation of what is missing or how this should work. I know I’m dropping the second number somewhere but, i can’t see it right now.

Hi,
I like your code, I did some stealing myself where you are stuck. I think you’re having some difficulty because you keep using arguments every time, How about you put all arguments into one array first
var parameters = [].slice.call(arguments);

then now coz you have an array, you can simply check if every element in the array is a number
var x = parameters.every(function(val) {return typeof val === “number”}); //x will be either true if all elements are numbers and false otherwise

if(x === false) {return undefined;} that way you will never have to confirm if both parameters are numbers again.

Now because all parameters are in one array
if(params.length === 2) {return params[0] + params[1]} //this part you already solved

so to the tough part
else{
//use the one parameter you have to recall the function
var param1 = parameters[0];

}

basically what this does is it runs addTogether(2) which will return 2, then afterwards run addTogether(3) which means it will now return 2 + 3 = 5. So basically the function will call itself a second time with 3 as the argument. And where 3 is not a number now it will simply return undefined.
It’s a bit confusing for me too but that is what closures do, they allow you to call a function again within the function and attach it to a variable. I found this part online, apparently it’s a common question in interviews.

Sorry I had to start from scratch, I have a hard time understanding people’s code.

1 Like

You also might want to read about spread operator. This is ES6, but it lets you easily make arguments into array.

1 Like

That’s exactly what I needed! Thanks so much, man. I had forgotten the recursive bit and I think that’s what did it for me.

Do you have a reason why I should need to plot the arguments into an array instead of just working with them directly?

I followed your advice on it but, I don’t really see the logic behind it.

Thanks again.

Hey