# Intermediate Algorithm Scripting - Arguments Optional

Tell us what’s happening:
Describe your issue in detail here.
Help! I need to know if the test is wrong or if it’s me who made a mistake in my code. All the tests pass except this one: ‘addTogether(5, undefined) should return undefined.’ Supposedly, if the function is passed only one parameter, it should return a function that takes a parameter. However, if a number is passed as the first parameter and UNDEFINED as the second parameter, it should return UNDEFINED, which is illogical because it would be the same as passing only one parameter. I need to know if I’m doing something wrong or if the test is incorrect.

``````function addTogether(a, b) {
if(typeof a !== 'number'){
return undefined
}
if(b===undefined) {
return function(c) {
if (typeof c !== 'number'){
return undefined;
}
return a + c;
}
}
if(typeof b !== 'number'){
return undefined
}
return a + b;
}

``````

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

Passing two parameters is not the same thing as passing one parameter, even if one of the two parameters is undefined

That fail case , seems like returning you a function instead of undefined.

Your if-else logic wrongly think it was :

1 possible solution is make your first if-else smarter, to handle this situation:

• return undefined when receive 2 inputs AND the 2nd input is undefined

and then it should passed.

This is what Chat GPT says.
" in JavaScript, when a function is called and no argument is provided for a parameter, that parameter is automatically assigned as `undefined`. Therefore, in the case of `addTogether(param1, undefined)`, JavaScript would interpret it as only one argument being passed and the second parameter not being provided.

In JavaScript, parameters that don’t receive a value are automatically assigned as `undefined`. Hence, `addTogether(param1, undefined)` is equivalent to calling `addTogether(param1)`.

If you want to ensure that two distinct parameters are provided when calling the function, you can use the additional check mentioned in my previous response to verify if any of the parameters is `undefined`."

This is false. ChatGPT is lying to you. The first sentence is fine but the second is wrong.

ChatGPT statistically guesses what it should say and has no way to check if what text it generated is correct.

The function call `addTogether(5)` and `addTogether(5, undefined)` are different function calls with different behavior. A correct solution needs to tell the difference between these two different function calls.

I would use Google (or whatever search engine you like) and not ChatGPT to research “JavaScript how to determine number of arguments”.

You modified the function signature from what was given in the seed code to explicitly assume that two arguments are given, and that is causing the heart of your problem here.

1 Like

Alright, if you say so, I understand. Now, I’m not sure how to implement it in the code. I tried doing it as WongYC-66 mentioned earlier, but it still doesn’t work. Maybe I’m doing something wrong. If someone could tell me what I need to modify, I would appreciate it.

if(typeof a !== ‘number’){
return undefined
}
if(a && b && b===undefined){
return undefined
}
if(!b) {
return function(c) {
if (typeof c !== ‘number’){
return undefined;
}
return a + c;
}
}
if(typeof b !== ‘number’){
return undefined
}
return a + b;
}

``````function addTogether(a, b) {
//If 'a' is not of type number, return UNDEFINED.
if(typeof a !== ‘number’){
return undefined
}
//If 'a' exists and 'b' exists and 'b' is exactly equal to UNDEFINED, return UNDEFINED.
if(a && b && b===undefined){
return undefined
}
//If 'b' does not exist, return a function.
if(!b) {
return function(c) {
//If 'c' is not of type number, return UNDEFINED.
if (typeof c !== ‘number’){
return undefined;
}
return a + c;
}
}
//If 'b' is not of type number, return UNDEFINED.
if(typeof b !== ‘number’){
return undefined
}
//if 'a' and 'b' are numbers, return the sum.
return a + b;
}
``````

if ‘a’ is 0 and ‘b’ is number return ‘b’, if b is 0 and ‘a’ is number return function but It should return the sum of ‘a’ + 0.

Did you do this? I don’t see the technique from the first few search results in your code.

1 Like

Thank you, I found the solution using `arguments.length` xD.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.