# Return a sum challenge

Tell us what’s happening:
Describe your issue in detail here.

i got everything to pass except these 2 lines of code. im not sure how to set up the addTogether(5)(7).

`addTogether(5)(7) should return 12. `
` addTogether(2)([3]) should return undefined`

``````   **Your code so far**
``````
``````
let sum = 0;

for(let i = 0; i < arguments.length; i++){
if(typeof(arguments[i])!=='undefined'  && typeof(arguments[i]) =='number'){
if(arguments.length<2){
}
sum+=arguments[i];

}else{
return undefined;
}

}

return sum;
}

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

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36`

Challenge: Arguments Optional

From the instructions:

“If only one argument is provided, then return a function that expects one argument and returns the sum.”

You aren’t returning a function in this case. Returning a function means to literally return a function you define in the code, such as:

` return function(arg) { /* do something in function */ };`

So if you originally call this function as:

`const sumTwoAnd = addTogether(2);`

The variable `sumTwoAnd` contains a function and that function should add 2 to whatever number you pass into it. Thus you can call the variable `sumTwoAnd` as a function using parens:

`const total = sumTwoAnd(3);`

And now `total` will equal 5.

My suggestion would be to work through each of the scenarios to make sure your function returns properly.

1. If either argument isn’t a valid number then return `undefined`.
2. If there are two arguments and they are both numbers then return the sum.
3. If only one argument is provided, then return a function that expects one argument and returns the sum.

Don’t worry about trying to write awesome code at this point, it can look ugly for now, just get it to work. Then you can go back after the fact and clean it up.

Good luck!

is my 2 if fstatements going in the right direction?

You pass some conditions, so yes you are heading roughly towards the right direction… But you are not returning a function anywhere.

I would remove the for loop.

yeah i thought of that to considering that i wont see any more than 2 arguments . no iteration is necessary

Your function needs to have different behavior when you only have one argument and when you have two arguments. A loop is for repeating the same behavior over again.

im having a hard time with returning the function for when i get one argument.
i figured out why. somehow when i set my if statement to test for that the length of the argument is less tahn 2, it never goes to the function to be returned. i have no clue how to set that up in order to return that function.

``````
var num1 = arguments[0];

var num2 = arguments[1];

let sum = 0;

if(typeof(arguments[0]) !=='number'){

return undefined;

}

if(typeof(arguments[1]) !== 'number'){

return undefined;

}

else{

sum=arguments[0]+arguments[1];

}

if(arguments.length <2){

return sumTwoAnd(arguments[0]);

}

return sum;

}

its skipping this part of the test

`if(arguments.length <2){`

Remember, the second argument might not be passed into the function, and if it isn’t then you would return a function. But this `if` is going to return `undefined` instead in that situation. In other words, the second arg might not be a number but it could legitimately be `undefined`.

You already have the first condition met, you return `undefined` if the first arg isn’t a number. Personally, I would next work on the second condition of when the second arg is also a number or something other than undefined. Once you have those two working properly then all you have left is returning the function.

1 Like

thanks alot , ill take a break and gather my thoights. apparently this challenge seemed very trivial at first but wind up a bit more challenging than i thought. still not as difficult as that steam roll recursion problem.

so i was reading through the desc again ,i guess i wasnt absorbing the author’s lingo but i guess when they say when one parameter is available , you return a function. well from your hints im getting that one parameter means that one of the numbers must be undefined. so a diffeent else statement to set up my function. but im getting the same results. heres my code.

`````` function addTogether() {
var num1 = arguments[0];
var num2 = arguments[1];
let sum = 0;

if(typeof(arguments[0]) !=='number'){
return undefined;

}else if (arguments[1] =='undefined'){
return sumTwoAnd(7);

}

if (typeof(arguments[1])!=='number'){
return undefined;
}
return arguments[0]+arguments[1];
}

``````

This is not how you check if something is `undefined`.

You are getting closer. Why do you think you should call the function again with 5 as the argument? Where did you get 5 from? And then why would you call the resulting function with 7? You can’t just stick random numbers in there

When only one number is originally passed in then the function you are returning takes one arg as well. From the rules of the challenge, if that arg is not a number then the function returns `undefined`, otherwise it returns the number passed in plus the number passed in from the original function call (which you have saved off as `num1`). So how would you create a function that meets that criteria? That’s the function you want to return. Remember, because you are creating a new function within the `addTogether()` function, the new function has access to all of the variables in `addTogether()`.

i was just playing around with tbhe test cases see if anything changes but apparently the function never gets called or returned as well

Ahh, I see. You are correct, that `if` block never executes because you are not checking for `undefined` correctly. You are checking for the string “undefined”.

1 Like

ok so i need to create another functon withiin the function that have access to all the elements , return it to that functoin and that function return itself to the main function. im assuming thats what your getting at.

Yes. The function you are creating takes one argument. If that argument is not a number it returns undefined, otherwise it returns the argument + the original number passed in to `addTogether`.

I’m not sure I’m following the logic here. Basically, you just want to return the new function you create. That’s it.

yes thats exactly what i meant. too much coffee and coding today

``````
var num1 = arguments[0];

var num2 = arguments[1];

let sum = 0;

if(typeof(num1) !=='number'){

return undefined;

}else if (num2 === undefined){

if(typeof(number2 !=='number')){

return undefined;

}else{

return num1+number2;

}

}

}

else if (typeof(num2) !=='number'){

return undefined;

} else{

return num1+num2;

}

}