I’ve worked, searched, tried different options. But no matter how I form the code, when it tests addTogether (2)(3) or addTogether (2)([3]) it returns “TypeError: addTogether(…) is not a function.” I did read the documentation for this error, but I don’t really understand it or why I’m getting it.
So here is my code. If I comment out the final else-statement, it just runs. The console.log doesn’t even come back. So I’m obviously doing something wrong there. It’s as if haven’t an else-statement at all is the problem.
function addTogether(a, b) {
//Function to check if an argument is a number
function isItNumber(arg) {
return typeof arg == "number";
}
var firstArg = arguments[0];
//If either arguments aren't numbers, return undefinesd
if(isItNumber(arguments[0]) == false || isItNumber(arguments[1]) == false) {
return undefined;
}
//If there are 2 valid arguments, return the sum of the 2 numbers.
else if (arguments.length === 2) {
return arguments[0] + arguments[1];
}
//If one argument given, return adding function.
else {
console.log("Program made it to else");
return function adder(x) {
return addTogether(x, firstArg);
};
}
}