Stuck on: Arguments Optional, help plz

Tell us what’s happening:

With my code as it is, I’m passing every test except:

“addTogether(2, “3”) should return undefined.”

Can I please get a hint as to what I should look into? I tried adding another “if else” statement that would return undefined if num1 is a number and num2 is not a number, but that didn’t work. I also know that generally chaining together else if statements isn’t really good practice.

I tried to utilize the “arguments object” method but wasn’t sure how to make it work for me.

Your code so far


function addTogether(num1, num2) {
if (typeof(num1) === "number" && typeof(num2) === "number"){
return num1 + num2
} else if (typeof(num1) !== "number" && typeof(num2) !== "number"){return undefined}

else return function(y){
   if (typeof(y) === "number"){
return num1 + y;} else return undefined
 }

}

var result = addTogether(2)(3);
console.log(result)

Your browser information:

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

Challenge: Arguments Optional

Link to the challenge:

You are returning undefined if num1 is not a number AND if num2 is not a number.

1 Like

Thanks, I changed it to this and it passed all the tests. Not sure if this is the ideal solution, so I’ll check the other solutions.

function addTogether(num1, num2) {
if (typeof(num1) === "number" && typeof(num2) === "number"){
return num1 + num2
} else if (typeof(num1) !== "number" || typeof(num2) === "string"){return undefined}

else return function(y){
   if (typeof(y) === "number"){
return num1 + y;} else return undefined
 }

}

var result = addTogether(2)(3);
console.log(result)
1 Like

This will pass the tests, but won’t properly work. See how I can break it if I pass an array as num2?

1 Like

I see, thanks. Is the solution a matter of adjusting the else if statement? Or is there an entirely different approach I should be taking?

(typeof(num1) !== "number" || typeof(num2) === "string")
It’s all in this line here. You check if num1 is something other than a number, which is good.
But why do you check if num2 is specifically a string?

1 Like
(typeof(num1) !== "number" || typeof(num2) !== "number")

I would have thought this would work, that if either num1 OR num2 or not numbers, return undefined, but it fails every test.

I’m gonna have to think about this some more.

Have you learned about the arguments variable yet??

1 Like

Thanks, I updated to the below which passes. Is this a proper solution? I’m checking it atm.

function addTogether(num1, num2) {
var Args = arguments.length;
if (typeof(num1) === "number" && typeof(num2) === "number"){
return num1 + num2
} else if (typeof(num1) !== "number" || Args !== 1){return undefined}
else return function(y){
   if (typeof(y) === "number"){
return num1 + y;} else return undefined
 }
}

Nice work! I wasn’t able to pass arguments to break it.

2 Likes

Thanks for the help! Appreciate you.

1 Like

Always happy to help! Now take some time to celebrate your success! You went above and beyond, and instead of just focusing on making your function pass the tests, you made it pass more potential user inputs. Nice work!

2 Likes