isNaN strange behavior

Tell us what’s happening:
Hello,

I am working on the section that wants me to create a function that sums two numbers. If either of the two arguments passed to the function are not numbers I am supposed to return undefined.

When I have just the code below, my function works just fine at filtering out all non numbers.

function addTogether() {
if (isNaN(arguments[0])||isNaN(arguments[1]))
  return undefined;
}

However once I add the else if statement

else if(arguments.length == 2){
  return arguments[0] + arguments[1];
}

It no longer weeds out some of the non number values and doesn’t return undefined for this statement.

addTogether(2, "3") should return undefined .

Why?

Thanks!

  **Your code so far**

function addTogether() {
if (isNaN(arguments[0])||isNaN(arguments[1])){
  return undefined;
}else if(arguments.length == 2){
  return arguments[0] + arguments[1];
}
}


addTogether(2,3);
  **Your browser information:**

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

Challenge: Arguments Optional

Link to the challenge:

Keep in mind undefined is the default value that’s returned by function, unless it explicitly returns something else.

When having:

function addTogether() {
if (isNaN(arguments[0])||isNaN(arguments[1]))
  return undefined;
}

Actually in every case function will return undefined. Having said that take a look how isNaN behaves with the string number - console.log(isNaN('3')).

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