# Intermediate Algorithm Scripting - Arguments Optional

Tell us what’s happening:
Describe your issue in detail here.
I am not understanding why isn’t the ‘else’ parts executing when I call for `addTogether(5)` and `addTogether(5, undefined)` ?

``````function addTogether() {
let sum_result = sum(arguments[0], arguments[1]);
// console.log(sum_result);
// console.log(isNaN(sum_result));
if (typeof (sum_result) == 'number') {
return sum_result;
}
else if (arguments.length == 1) {
return sum;
}
else {
return undefined;
}
}

function sum(param1, param2) {
return param1 + param2;
}
``````

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

Challenge: Intermediate Algorithm Scripting - Arguments Optional

``````typeof NaN
'number'
``````

Thank you. Made this change but my code isn’t passing for two test cases. I am not understanding the way the test cases are passed.
Why is 5 and 7 passed with 2 separate brackets in `addTogether(5)(7)` ?

This is the updated code screenshot:

Post the code not images.

Use the “preformatted text” tool in the editor (`</>`) to add backticks around the code.

Ok thank you for guiding.

``````function addTogether() {
let sum_result = sum(arguments[0], arguments[1]);
// console.log(sum_result);
// console.log(isNaN(sum_result));
if (typeof (sum_result) == 'number' && !isNaN(sum_result)) {
return sum_result;
}
else if (arguments.length == 1) {
if(typeof (arguments[0]) != 'number') {
return undefined;
}
else {
return sum;
}
}
else {
return undefined;
}
}

function sum(param1, param2) {
return param1 + param2;
}
``````

The way that 5 and 7 are passed in `addTogether(5)(7)` is called “currying”.

You can think of `addTogether(5)` as a function itself and `(7)` as the argument being passed to that function because that’s exactly how you’re supposed to solve this:

`addTogether(5)` is supposed to return a function that can take another argument and then return a final sum when there are no more arguments to go.

Basically, you are returning a function but you aren’t returning a function that meets the requirements.

Yes. I made changes in the function to be returned but still that test case is not passing. I am not understanding what is actually asked to do. This is the updated code:

``````function addTogether() {
let sum_result = sum(arguments[0], arguments[1]);
// console.log(sum_result);
// console.log(isNaN(sum_result));
if (typeof (sum_result) == 'number' && !isNaN(sum_result)) {
return sum_result;
}
else if (arguments.length == 1) {
if(typeof (arguments[0]) != 'number') {
return undefined;
}
else {
return sumOne(param);
}
}
else {
return undefined;
}
}

function sum(param1, param2) {
return param1 + param2;
}

function sumOne(param) {
return param;
}
``````

Thank you for explaining.

I would use this sort of check when there are two arguments.

This also doesn’t have the required checks that the instructions ask for.

Thanks.
I wrote the code again after understanding and this time it passes this testcase: `addTogether(5)` should return a function.

I fail in the currying testcases:

• `addTogether(5)(7)` should return 12.

• Failed: `addTogether(2)([3])` should return `undefined`

My code so far:

``````function addTogether() {
if(arguments.length === 1) {
if(typeof(arguments[0]) === 'number' && !isNaN(arguments[0])) {
return sumOne;
}
else if(typeof(arguments[0]) !== 'number') {
return undefined;
}
}
else if(arguments.length === 2) {
if(typeof(arguments[0]) !== 'number' || typeof(arguments[1]) !== 'number') {
return undefined;
}
else if(typeof(arguments[0]) ==='number' && !isNaN(arguments[0]) && typeof(arguments[1]) === 'number' && !isNaN(arguments[1])) {
return sumTwo(arguments[0], arguments[1]);
}
}
// return false;
}

function sumOne(param) {
return param;
}

function sumTwo(param1, param2) {
return param1 + param2;
}
``````

I’ll learn currying and try.

Your function here is not doing any checking of the param, and it isn’t adding the param to the first number.

You’re going to need to define this `sumOne` inside of your `addTogether`.

sorry but what should I sum the only parameter with.
I mean, sumOne(5);
what should I add to 5 ?

You need to add the `param` to the value given to `addTogeather`

yes sir. But I am given 5 as the parameter to `addTogether` that’s why I am not understanding which number to add to it

You add the `5` passed to `addTogether` to the `param` here

You mean to say putting `addTogether(5)` inplace of param in the function definition of `sumOne` ??

Nope. I mean adding the value passed as the first argument to `addTogether` to the value passed as the `param` for your `sumOne` function.

For that to work, you need to create your `sumOne` function inside of the `addTogether` function.

Could you please have a lot in these two code samples.
1st sample gives o/p as 5
2nd gives an error: SyntaxError: unknown: Unexpected token, expected “,” (22:27)

``````function addTogether() {
// function kay karta: sums two arguments.
// samajh ekach argument dili asel tar teh ek function return karaycha jechyaat ek parameter asel aani teh sum return karel.
if(arguments.length === 1) {
if(typeof(arguments[0]) === 'number' && !isNaN(arguments[0])) {
return sumOne(arguments[0]);
}
else if(typeof(arguments[0]) !== 'number') {
return undefined;
}
}
else if(arguments.length === 2) {
if(typeof(arguments[0]) !== 'number' || typeof(arguments[1]) !== 'number') {
return undefined;
}
else if(typeof(arguments[0]) ==='number' && !isNaN(arguments[0]) && typeof(arguments[1]) === 'number' && !isNaN(arguments[1])) {
return sumTwo(arguments[0], arguments[1]);
}
}
// return false;

}

function sumOne(param) {
return param;
}

function sumTwo(param1, param2) {
return param1 + param2;
}

``````

Vs

``````function addTogether() {
// function kay karta: sums two arguments.
// samajh ekach argument dili asel tar teh ek function return karaycha jechyaat ek parameter asel aani teh sum return karel.
if(arguments.length === 1) {
if(typeof(arguments[0]) === 'number' && !isNaN(arguments[0])) {
return sumOne(arguments[0]);
}
else if(typeof(arguments[0]) !== 'number') {
return undefined;
}
}
else if(arguments.length === 2) {
if(typeof(arguments[0]) !== 'number' || typeof(arguments[1]) !== 'number') {
return undefined;
}
else if(typeof(arguments[0]) ==='number' && !isNaN(arguments[0]) && typeof(arguments[1]) === 'number' && !isNaN(arguments[1])) {
return sumTwo(arguments[0], arguments[1]);
}
}

function sumOne(arguments[0]) {
return param;
}

}