Arguments Optional I passed it but question about return undefined

Tell us what’s happening:
I was happy when I passed the test and decided look at if there is a better way of writing the code. When I looked at the solution, it had if-else statement that returns undefined.
When I wrote my code, I decided not to specifically return undefined.
Question is, Do I need to specifically return undefined? or can I just leave the code as is (without returning undefined if false)

thank you!

Your code so far


function addTogether() {
  var num1 = arguments[0];
  var num2 = arguments[1];

  // if there are 2 arguments,
  if (arguments.length == 2){
    // check if the type of num1 and num2 is Number,
    if (typeof num1 === "number" && typeof num2 === "number"){
      // add and return the value
      return num1 + num2;
    }
  }
  // otherwise, if there is 1 argument(s),
  else if (arguments.length == 1){
    // check again if the first argument is a type of Number
    if (typeof num1 === "number"){
      // return a function that asks to input another number
      return function(n){
        // if the type of inputted value is Number,
        if (typeof n === "number"){
          // add and return the value
          return num1 + n;
        }
      }
    }
  }
}

addTogether(2,3);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional

@rtdk0324,

whether you want to return undefined or not, you have to ask yourself about the behavior you want this method to have. Meaning, is it clear to the user that he/she must pass two parameters or not? If not, then how do you want to handle those scenarios? It is a deeper subject and I won’t get into it here, but you just have to keep in mind how to handle erroneous behavior (with exceptions, specific return types).

Hope that explains a bit.

1 Like

gotcha,
I guess it depends on situations