Arguments Optional : addTogether(2)(3) HELP!

Hi All! I need some help with my code for “arguments optional” challenge. I keep getting “TypeError: addTogether(…) is not a function” error for case addTogether(2)(3); Where have I gone wrong???

function addTogether() {
 function sumTwoAnd(num){      // function to use if only one argument.  
    return 2+num;
  } 
  
  var temp = "";   // need a var               
  
  if (arguments.length == 2  && typeof arguments[0] === "number" && typeof arguments[1] === "number"){  // if there are two agruments and they are numbers then add them
     temp = arguments[0]+arguments[1];   
  } 
  else  if (arguments.length == 1 && typeof arguments[0] === "number"){  // if only one argument and it is a number then add it to 2
    temp = sumTwoAnd(arguments[0]);         
  }
  else{                                    // all other cases are undefined
    temp = undefined;
  }
  console.log("temp = "+temp);
  return temp;
}
1 Like

When two sets of arguments are given, addTogether needs to return a function. The second set of arguments will then become the arguments of this returned function.

I’d like to also point out a logical error. You have the function sumTwoAnd which will always add 2 to the argument. This is incorrect. When two sets of arguments are given, the sum of those two arguments should be the result.

Thanks ArielLeslie, i guess I’m not understanding the challenge fully. Looks like I also need to read up on functions that return functions - a bit alien to me at the moment.

Will we ever see this type of function out in the wild?

This error came up since in one of my if statements, I was checking to see if the 2nd argument was a number but the function running the 1st time around didn’t have a 2nd argument. I changed the if statement to include a check to see if there is a 2nd argument and that solved the issue after spending half a day on this.

What do you mean by “this type of function”? One that behaves differently depending on how many parmeters it gets? Absolutely. One that returns a function? Less often, but yes.