Arguments Optional - addTogether(...) is not a function problem

Tell us what’s happening:

If I run the code below with addTogether(2,3); I end up passing 3/5 tests.
Failed tests being:
addTogether(2)(3) should return 5.
addTogether(2)([3]) should return undefined.

If I run the code below with addTogether(2)(3); I end up failing all 5 tests.

Console is outputing: addTogether(...) is not a function

Where did I go wrong?

Your code so far


function addTogether() {
  let param1 = arguments[0];
  let param2 = arguments[1];

  if (typeof param1 !== "number" || typeof param2 !== "number") {
    return undefined;
  }

  if (arguments.length === 2) {
    return param1 + param2;
  }
  else if (param2 === undefined) {
    return function x (num) {
      if (typeof num !== "number") {
        return undefined;
      }
      else {
        return param1 + num;
      }
    }
  }
}

addTogether(2,3);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36.

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

In order for addTogether(2)(3) or addTogether(2)([3]) to work, addTogether(2) must return a function.

Can you please elaborate what do you mean by returning a function?

Didn’t I return it with: return function x (num) {?

But you have this:

if (typeof param1 !== "number" || typeof param2 !== "number") {
    return undefined;
  }

So you return undefined instead of a function.

My bad, still learning.
Thank you for your help, I’ve managed to solve the challenge.

function addTogether() {
  let param1 = arguments[0];
  let param2 = arguments[1];
  let sum = 0;
  let params = [].slice.call(arguments);
  console.log(params)

  if (!params.every(function(param){
    return typeof param === "number";
  })){
    return undefined;
  }

  if (arguments.length === 2) {
    sum = param1 + param2;
  }
  else if (arguments.length < 2) {
    sum = function addTogether (num) {
      if (typeof num !== "number") {
        return undefined;
      }
      else {
        return param1 + num;
      }
    }
  }

return sum;
}


addTogether(2)(3);
1 Like

Congratulations on figuring it out. Happy coding.