Intermediate Algorithm Scripting - Arguments Optional

If the OR is used it does not work. If the “second” variable is check with “typeof” later in the code, it does work.

Why does it not work as an OR but the code does work with each their own IF statement?

More specifically: the test case addTogether(2)([3]) does not work. I would expect it to return undefined, but it doesn’t.

My code so far

function addTogether() {
  let [first, second] = arguments; 
//here the OR statement does not solve the challenge, but a separate IF statement below does solve the challenge 
  if (typeof(first) !== "number" || typeof(second) !== "number")
    return undefined; 
  if (arguments.length === 1) 
    return (second) => addTogether(first, second);
// comment: if I use the code commented out below, it does work as expected and solves the challenge 
  // if (typeof(second) !== "number") 
  //   return undefined; 

  return first + second;
}

addTogether(2,3);

Challenge: Intermediate Algorithm Scripting - Arguments Optional

Link to the challenge:

If there is only one argument, the second argument is undefined, which is not going to be of type "number"

1 Like

the test case addTogether(2)([3]) does not work, though. I would expect it to return undefined, but it doesn’t

Right, because you return undefined from addTogether(2) instead of a function

1 Like

If a value was returned, why did 1 function call affect another function call?

I added console.log() in a few places to try to understand why this was happening. Notice it’s returning “undefined” for addTogether(2)([3]) like it should, BUT Free Code Camp is saying this is wrong.

function addTogether() {
  let [first, second] = arguments; 
//here the OR statement does not solve the challenge, but a separate IF statement below does solve the challenge 
  if (typeof(first) !== "number" || typeof(second) !== "number"){
    console.log("undefined for " + first + " and " + second); 
    return undefined; 
  }
  if (arguments.length === 1) {
    console.log("called again for " + "second"); 
    return (second) => addTogether(first, second);
  }
// comment: if I use the code commented out below, it does work as expected and solves the challenge 
  // if (typeof(second) !== "number") 
  //   return undefined; 
  console.log("Adds " + first + " and " + second); 
  return first + second;

}

addTogether(2,3);

My Console output:

Free Code Camp Test Case Checking

Why is Free Code Camp saying the test case failed if the console is saying it returned undefined like it should?

The tests say its wrong because it is wrong.

If only one argument is provided, then return a function that expects one argument and returns the sum.

With a correct solution,

console.log(typeof(addTogether(2))); // function

But with the solution that prematurely checks if typeof(second) !== "number"

console.log(typeof(addTogether(2))); // undefined

The syntax

addTogether(2)([3]);

says that addTogether(2) returns a function, and then that function is called with the argument [3].
In other words

const addTwo = addTogether(2);
const finalAnswer = addTwo([3]);