I can't undefine non-numbers

Tell us what’s happening:
This is what I need to do:
I need to find two numbers and sum them;
If “sum” is only one number I have to create a function, like : “sum + y”;
If the length of “sum” is bigger than 2, then it will ignore the other numbers;
If it’s not a number, return undefined;

This is my Problem:
-I can’t return “addTogether(2)([3]);” as undefined, it throws an error;

  • The “addTogether(2, “3”);” sums 2 + “3” like “3” was a number, it should return undefined;
  • I can’t make “addTogether(2)(3)” be considered as a number, I don’t know how to get rid of these “(”;
    -For some reason the code output is:
    undefined
    2 + y
    undefined
    undefined

My Code so far


function addTogether(...sum) {
if (isNaN(sum)){  
  return undefined;
}
else if (sum.length >= 3) {
  return sum[0] + sum[1];
}
else if (sum.length === 2) {
  return sum[0] + sum[1];
}
else if (sum.length === 1) {
  return sum + " + y"
} 
return sum;

}

//console.log(addTogether(2)([3]));
//console.log(addTogether(2)(3));
console.log(addTogether(2, 3));
console.log(addTogether(2));
console.log(addTogether(2, 5, 6, 7));
console.log(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/81.0.4044.129 Safari/537.36.

Challenge: Arguments Optional

Link to the challenge:

Hi,

I was blocked with this exercise too at the moment.

When there are one function but with two parenthesis one is done after the other, then in the first moment you must save the function number in the first value.

addTogether(2)(3)

Then you mus do something like, save the value passed for the first function and after if everythign is ok, return a function that must be filled with the first value and the second in the parenthesis


var saveValueForNextFunction = value[0]
/*If value in the first is number you save it for later*/
else if (sum.length === 1) {
  saveValueForNextFunction = value[0]
 return function(bfunction){

        if(checkNum(bfunction) == true){

          valueToreturn = saveValueForNextFunction + bfunction
          return  valueToreturn
        }
} 

You can find further information in here

I hope that i helps

1 Like

it solved my problem, but now addTogether(2)([3]) and addTogether(2, “3”) returned “23” and “23”;
How can I return them undefined?

You must check before the return if its a number, if is string or Object return undefined, else continue with the function

1 Like

Thanks a lot! I am going to search about it.

1 Like