4

I'm currently working on a JavaScript exercise in FreeCodeCamp, and one of the test-cases my code should work with is a function call that looks like this:

addTogether(2)(3);

here is the bare-bones function I'm given:

function addTogether() {
  return;
}

When I run the code below:

function addTogether() {
  return arguments;
}

In the console, that the editor provides, I get:

TypeError: addTogether(...) is not a function

The instructions hint at using the arguments object, and it works well with test-case function calls that only have one argument object (i.e. addTogether(2, 3);), but not with the one I've shown above.

Is there a way to access/utilize the separate argument objects when they're in the format I showed above?

Note: I don't want any sort of answer to solve the problem, just info on any techniques on accessing the arguments of these type of function calls.

Help, is greatly appreciated.

Jaquarh
  • 6,493
  • 7
  • 34
  • 86
Edson
  • 255
  • 1
  • 3
  • 18
  • repeated question [http://stackoverflow.com/questions/29431006/javascript-functioncallarg1arg2](http://stackoverflow.com/questions/29431006/javascript-functioncallarg1arg2) – Canilho Oct 12 '16 at 20:00
  • 2
    "I don't want any sort of answer to solve the problem, just info on any techniques on accessing the arguments of these type of function calls" Just gotta say, I appreciate the attitude. Too many people just want a quick solution to their problem without actually understanding the solution. Kudos. – Mike Cluck Oct 12 '16 at 20:13

3 Answers3

4

Don't think of it as two separate sets of arguments. Think of it as you're calling another function (which you are). Functions are first-class values in JavaScript so you can use them just like any other value. This includes returning them from functions.

var f = function(y) {
  console.log('f:', y);
};

var getF = function(x) {
  console.log('getF:', x);
  return f;
};

getF(1)(2);

Functions can also use values that exist in any parent scope. Loosely speaking, functions which do this are called closures.

function createGetX(x) {
  return function() {
    // Since x is in the parent scope, we can use it here
    return x;
  }
}

var one = createGetX(1);
console.log(one()); // Always
console.log(one()); // returns
console.log(one()); // one
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
1

Sometimes it helps me to think in a certain order. You can read the code "addTogether(2)" and stop before reading the "(3)".

From there you can see the exercise wants you to have that first part "addTogether(2)" return a function...since anytime there are "()" that means a function is getting called.

So "addTogether(2)" needs to return a function that takes one argument. Later that 3 will be one example of an input.

I think the name "addTogether" is a bit confusing..since that function's job is to make up and return the actual function that adds.

Sort of hard to explain this one without helping too much, but the bulk of your job here is to return a custom function, which includes the first variable (from it's scope) and expects another variable when it's called.

mat
  • 191
  • 1
  • 5
0

You could do this with closures.

function addTogether(x) {    
  return function(y) {
     return x+y;   
  }   
}
Fida
  • 1,339
  • 1
  • 12
  • 21