TypeError on Arguments Optional

Tell us what’s happening:
Hello everyone, I can’t figure out why my code gives me an error message :

“TypeError: addTogether(…) is not a function”

Can someone help me ?
Thanks in advance !
Your code so far


function addTogether() {

for (let i = 0; i < arguments.length; i++) {
  if (typeof(arguments[i]) !== "number") {
    return undefined;
  }
}

if (!arguments.length < 2) {
  return arguments[0] + arguments[1];
} else {
  let arg1 = arguments[0]
  return (arg2) => {
    if (typeof(arg2) !== "number") {
      return undefined;
    } else {
      return arg1 + arg2;
    }
  }
}
}

addTogether(2)(3);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36.

Challenge: Arguments Optional

Link to the challenge:

try looking at it with this tool:
http://pythontutor.com/javascript.html#mode=edit

you will see what happens

but I don’t know why (maybe it is for the arguments object…)

1 Like

Take a look at this condition:
if (!arguments.length < 2)
Way it’s written now, this always will be true.

2 Likes

This works !

function addTogether() {

  for (let i = 0; i < arguments.length; i++) {
    if (typeof(arguments[i]) !== "number") {
      return undefined;
    }
  }

  if (arguments.length === 2) {
    return arguments[0] + arguments[1];
  } else {
    let arg1 = arguments[0]
    return (arg2) => {
      if (typeof(arg2) !== "number") {
        return undefined;
      } else {
        return arg1 + arg2;
      }
    }
  }
}

addTogether(2, 3);