Intermediate Algorithm Scripting - Arguments Optional

I could use some help understanding the “typeof” operand.

If I remove “typeof” from the second if statement this code passes the test but I don’t understand why. If the second variable is undefined shouldn’t “typeof” return undefined and still trigger the if statement.

Instead of working I get TypeError: addTogether(…) is not a function

  **Your code so far**
function addTogether() {
const [first, second] = arguments;

if (typeof(first) !== "number")
  return undefined;
if (typeof(second) == undefined)
  return (second) => addTogether(first,second);
if (typeof(second) != 'number')
  return undefined;
return first + second;


  **Your browser information:**

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

Challenge: Intermediate Algorithm Scripting - Arguments Optional

Link to the challenge:

Because typeof returns a string. So if there is no value passed in for second then its value is undefined but typeof(undefined) is the string “undefined”, and in JS the string “undefined” does not equal the value undefined, even using “loose” equality.

Wow, I can’t believe I missed that. It seems so obvious once pointed out.

Thanks for the help!

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