Explanation needed-Arguments Optional

hi, i tried to figure out my own version of this problem and my intention was to check, as first thing to do, if either the arguments were NaN, after that branching the problem for the 2 arguments or one argument case. But it is not working…can someone explain me why?
this is the code

function addTogether() { 

  if (typeof arguments[0] !== "number"
     ||typeof arguments[1] !== "number" ) {    

      return undefined;    

  } else if (arguments.length === 2) {    

      return arguments[0]+arguments[1];           

  } else if (arguments.length === 1) {    

      let first = arguments[0];           

      function addSecond(second) {

        // Check if the new argument is a number

        if (typeof second ==="number") {

          return first + second;

        } else {

          return undefined;

        }

      }

      return addSecond;   

    }  
 }

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

here you have this, but if this is true, you have first this that is true

as typeof undefined is not "number"

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.