Arguments Optional algorithm. Feedback/question on my solution

Hi I just completed the “Arguments Optional” Question. I happen to solve it by adding a parameter to the function given. I was just wondering if that is okay to do? Otherwise I did add a comment line if I omitted it. Anyhow I was just curious what you guys think of this solution?

Summary
function addTogether(x) {
 // split arguments into array
 var args = Array.prototype.slice.call(arguments);

  // let x = args[0]; only if free code camp wants no parameters. 
 
  // check if the argument has a length of 2 and the 2nd argument is a number.
  // Technically I know the test subject is only passing non numbers to the 2nd place.
  // So I can also add another && to make sure argument 0 also is a number. 
  if(args.length == 2 && typeof args[1] == "number"){
    return args[0] + args[1];
  // if not 2 numbers check if it's only 1 argument then use a curried function to return function.
} else if(args.length == 1 && typeof args[0] == "number") {
      return function (y){
        // if 2nd argument is a number return the sum. Else return undefined. 
        if(typeof y == "number"){
          return  x + y;
        }
        else return undefined;
    }
  } else{
    return undefined;
  }
}
 
addTogether(2)([3]);

Does it work? Yes
Can I follow it? Yes
Good to go!

1 Like