Arguments Optional: Problem returning function

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);


I figured it out! It only took me hours and hours! I ended up moving around the conditions. I dealt with the instance where only one argument is passed first. Then I dealt with 2 arguments. In pseudocode it went like this

If only one argument and that one argument is a number, return the function.
Else if both are numbers add them together.
If any arguments aren’t numbers, return undefined.

I have to say, I did so much reading and video watching about closures I felt like I understood them, but couldn’t make it work out in my code. I’m not 100% sure why my final solution worked and the one I posted didn’t, but I’m happy it did. I’d still be interested why the above code failed at the final else-statement if anyone has any insight.

1 Like

You have an OR (||) operator here, so if either one of these sides evaluates to true, you will return undefined. If you only put in one argument, you’re already shorting it to return undefined rather than continuing in the program. You could change the OR to an AND (&&) and it would work.