addTogether(5)(7) <--- what is this notation for calling a function...?

Tell us what’s happening:
The editor says TypeError: addTogether(…) is not a function whenever I call a function with this notation. I’ve never seen this notation before, I have no idea what it means, but apparently it’s supposed to be an acceptable function calling notation…? Can someone elaborate on what this is and why the editor is saying its in error? Basically my code works for all the answers except the ones that use this notation that I’ve never seen before and have no understanding of what it means or is doing.

Your code so far


function addTogether() {
if (typeof arguments[0] !== "number") {return undefined} 
if (typeof arguments[1] !== "number") {return undefined} 

var c = arguments[0]

if (arguments[1]){
  return arguments[0] + arguments [1]
} 

return function(arg2){
  if (typeof arg2 ==="number") {
    return c+arg2
    } else {return undefined}
  }
}
 

addTogether(5)(7);



Your browser information:

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

Challenge: Arguments Optional

Link to the challenge:

addTogether(5)(7) indicates that addTogether(5) is expected to return a function. 7 will then be passed into that function as the argument.

1 Like

Expanding on what @ArielLeslie stated above. It is a shorter way of doing the following:

const returnedFunction  = addTogether(5);
returnedFunction(7);
1 Like

In this case, you can also use

let newFunc = addTogether(5);
let result = newFunc(7);

The issue with your code is with this line:

With my expanded syntax above, I think its a bit clearer to see that you will sometimes call addTogether with one argument instead of two. In this case, arguments[1] will be undefined and therefore not of type "number".

I don’t understand, its really hard for me to see or understand any of that if the code editor automatically flags the error code if I call anything using that format. Can you either give me a fixed code, or give me a way to rewrite the function call so I can fool around as intended? Cause atm I can’t see anything when using the code editor.

You have an error because you are returning the wrong result in the case of a single argument. You should be returning a function but you are instead returning undefined.
Check out a live example here.

2 Likes

Okay I understand, was able to delete that one line and fix the code. I still don’t understand this notation though of ‘calling a function within a function from outside the function’s function’?

addTogether(5)(7)

Is there another way to rewrite the function call? I understand what we are doing, and how we get 13 from this, but the idea of calling a function within a function from outside the function’s function…is something I need further documentation on I think. Is there a name for this kind of function call? Does this notation only work if I’m returning a function from within the first function?

I think that you are maybe overthinking this. You can return anything from a function, to include a function. Once you have that return value, you can use it.

// Let's make a function that returns a function
function adder(val) {
  function addVal(a) {
    return a + val;
  }
  return addVal;
}

// Let's make a function that adds 5
const add5 = adder(5);
console.log(add5);

// Let's call our function
console.log(add5(3));
2 Likes

I think I understand most of it now.

Is there a way to access the addVal if you remove the “return addVal” line? Like if I wanted to compute adder(3)(2) = 5 could I rephrase the way I call the 2 functions and make it work without that “return addVal” line?

I just find it really weird I could have run across adder(3)(2)(4)(22)(553) as a function call (assuming we edited the code to have more functions within functions) and for it not to have a specific name and to be considered normal when its never comeup until this point in my learning of js.

You can’t access anything outside of the function that you did not return out (unless you are modifying global variables, which I generally don’t recommend). The {}s trap those objects declared inside of your function (to include other functions) inside of that scope, and you can’t access them outside of that scope unless you pass them out.

Part of what the intermediate algorithms section does is helps you broaden full implications of things you have previously covered.

For instance, the ideas that

  1. A function is called with arguments in ()s

  2. A function call gets populated with it’s return value let myVar = myFunc(myArgs);

  3. A function can return a function (mentioned in the challenge description)

Can be combined, and these three ideas mean that

adder(3)(2)

is valid JavaScript syntax.

It’s a good thing that you were able to combine this knowledge and build a solution to the challenge - this means you are learning!

It’s just not possible to cover every possible way to combine the logical components of code, so we try to make challenges that build you up to being able to combine these ideas for yourself.

A big part of being a software developer is learning how to combine old ideas in new ways - they don’t pay us to write the same stuff that’s already been written!

The concept that is being introduced here is that functions are “first class citizens”. They can be stored in variables, passed into functions as arguments, and returned from functions. Maybe there is a particular name for this, but I haven’t run across it. This is the first time that freeCodeCamp introduces the concept, but you will continue to use it a few more times as you go forward.

For the record, this doesn’t rely on a function being inside another function. The key is just returning a function as a value.
For example, you can do this:
Check out a live example here

2 Likes

wowo didn’t know we could embed living images :slight_smile:

that’s a nice thing!

PS: Works super slow for me though, and seems to stay listening with no way to stop it.

It’s a pretty nice feature for repl.it embeds offered by the forum software.

I don’t know how to evaluate performance but this particular page is getting very slow…

I would guess that your internet connection is struggling to handle the two embeded repl.it links.

I switched the embeds to links. Hopefully that helps.

1 Like