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. Your code so far
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;
}
addTogether(2, undefined);
Your browser information:
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
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.
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.
function addTogether(a, b) {
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;
}