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: