Arguments Optional question

I don’t get the Arguments Optional challenge. When i try to do something with this code:
function addTogether() {
return false;
}

addTogether(2)(3);

It says that AddTogether is not a function. Can anyone please explain this case?

The problem wants you to create a closure, which is essentially a function that returns a function(s). So it throws an error because it expects addTogether() to return a function not a boolean variable.

Thanks! This code:
function addTogether() {
return function() {
return arguments;
};
}

addTogether(2)([3]);
Returns only second argument, in this case {“0” = [3]}. How can I get the first argument?

The issue is that there are two functions happening here, addTogether (whose arguments is just 2) and the unnamed function you’re returning (whose arguments contains [3]). Ultimately, you want addTogether() to return a function that does something with its own argument. Keep in mind that you can stick anything between the parentheses of that anonymous function to represent that argument. (I’m trying to keep the hints light to avoid spoilers, but let me know if you need more.)

Oh, also, check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments closely, because unless there’s some way around it that hasn’t occurred to me, you’re going to need to do some checks to arguments that you can’t without converting it into a real array.

Thank you. I did this:

function addTogether() {
var args = Array.from(arguments);
return function() {
return args += Array.from(arguments);
};

}

addTogether(2)([3]);

And now it returns 23. So It’s ok now. if I get something like this: addTogether(2)([3])(5); I need 3 functions to get 3 arguments?

There is some way around it that hasn’t occurred to you.:wink: Here is my solution, without the use of arrays:

function addTogether() {
  function checkIfNumber (num) {
    return Number.isInteger(num) ? num : undefined;
  }
  
  var a = checkIfNumber(arguments[0]);
  var b = checkIfNumber(arguments[1]);
  
  if (arguments.length > 1) {
  return checkIfNumber(a + b);
  } else if (a) {
    return function(newArg) {
      return addTogether(a, newArg);
    };
  }
}
2 Likes

Thks for the material