Hello folks, I have tried the following solution to the Arguments Optional challenge. I am passing all tests except for the currying ones - addTogether(x)(y);.
function addTogether(...arg) {
//Using rest parameter to get an array of arguments
//Check if both arguments are non-integer
if (!Number.isInteger(arg[0]) || !Number.isInteger(arg[1])){
return undefined;
//If there are two arguments - sum them
} else if (arg.length == 2){
return arg[0] + arg[1];
//If there is one argument - return a function
} else if (arg.length == 1){
return function(x) {
if (!Number.isInteger(x)){
return arg[0] + x;
} else {
return undefined;
}
}
}
}
I get TypeError: addTogether(...) is not a function.
I cannot understand why my internal function is not working.
I checked any of the two arguments since the problem asked “If either argument isn’t a valid number, return undefined.”
On the side note: yes, I can use typeof per your suggestion.
But I am still unsure why addTogether(5)(7); would not work in my solution.
I think I understand you now . My first if statement failed my solution, because a single argument would return !Number.isInteger(arg[1]) and undefined?
Here is my passing solution:
function addTogether(...arg) {
//Using rest parameter to get an array of arguments
//Function checking if argument is a number
function isNumber (a){
if (typeof(a) == "number"){
return true;
} else {
return false;
}
}
//If there are two arguments - check and sum them
if (arg.length == 2){
if (isNumber(arg[0]) && isNumber(arg[1])) {
return arg[0] + arg[1];
} else {
return undefined;
}
} else if (arg.length == 1){
if (isNumber(arg[0])) {
return (x) => {if(isNumber(x)) {
return arg[0] + x;
}}
}
}
}
Not the most elegant one, but it helped me understand the problem in a way slightly different to the other solutions.