Arguments Optional not understanding one part

Hi all.

Here is my solution below. I am not understading this part. How does this code work?

return (b) => addTogether(a,b)
  **Your code so far**

function addTogether(a,b) {
if (typeof(a) !== "number") {
  return undefined
}
if (b === undefined) {
  return (b) => addTogether(a,b)
}
if (typeof(b) !== 'number') {
  return undefined
}
return a+b;
}

addTogether(2,3);
  **Your browser information:**

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

Challenge: Arguments Optional

Link to the challenge:

Can you try to give us your understanding of what this line is doing? And then we can help you fill in the gaps if needed. You were able to solve the challenge so you must have some idea of what is going on here or how did you code this in the first place :wink:

I took a look at the solutions lol

I guess I am not understanding the parameter of the arrow function.

For the test case addTogether(5)(7), how does the parameter b in
return (b) => addTogether(a,b)
return the number 7? How does one argument become 2 arguments?

It’s pretty hard to understand the answer you copied if you don’t understand the question.

So, let’s back up and break down the question instead.

Do you know what this syntax is saying? What is the result of the function call addTogether(5)?

So to be able to understand what you need to do in this challenge, refer to this challenge in Functional Programming about currying/partial applications

essentially Javascript allows you to do part of what you want to do in a function, return a callback for when you have some other data to perform more calculations etc in the function.

A possible real world scenario, you are fetching multiple pieces of data from other places that is not within your own hosted server. You don’t know in what order or when the data will arrive, but you do know what the user wants needs all of the data as input.

So instead of demanding the browser to stop everything else and wait on that one function, say sum(a,b,c) to get a, b and c, which will result in your webpage hanging, you can call back the function each time you get a piece of data, a, b, or c.

addTogether (5)(7) invokes
if (b=== undefined)
since there is only one argument given.

From there, the code given invokes the recursion to add the first argument along with the parameter.

But how is the parameter in the arrow function recognizing the second number as 7?

Ignore the solution you copied.

What do the instructions say should be returned if you call addTogother with a single number as the argument?

It should return a function that adds the sum of the argument

Yes, a function is being returned. We can write that function lots of different ways but it is important to remember that it’s just a function.

What does this mean? Which argument? We have two different functions now.

There would be two different functions for 5 and 7 so the sum of the result of the two functions?

No. You can’t add functions together.

This syntax right here calls addTogether with the argument 5, gets a function back as a return value, and immediately calls that function with the argument 7

You can rewrite

addTogether(5)(7)

as

const returnFunc = addTogether(5);
const sum = returnFunc(7);

Do you see how this is saving the function returned from addTogether in a variable and then using that function to pass in the second number? The thing to remember is that functions can be passed around like any other type of value (such as a number or a string). So they can be returned from a function and stored in a variable just like you would return a number from a function. And once a function is stored in a variable it can be used by adding the parens on the end, just like you would use any other function.

1 Like

This isn’t a valid line. You just can’t do that.


Again… Ignore the code you copied. Pretend it never occurred to you to copy the answer. If you don’t understand the problem statement, then you will not be able to understand the solution, with or without copying, and you won’t be able to create a solution on your own.


If addTogether receives one argument, then it must return another function.

()s are how you call a function.

Therefore

addTogether; // Our original function 

// Style 1
const addFive = addTogether(5); // Returns a brand new function 
const sum = addFive(7); // Calls this new function 

// Style 2
const sameSum = addTogether(5)(7); // Takes the function returned from addTogether function call and immediately invokes it

Both chunks of code do the same thing. We call addTogether with one argument, get a function out, then call that function with another argument.

Can you write a function that does just this part? Don’t worry about the solution. Can you write a function that takes an argument, a, and returns a new function that takes a different argument, b, and returns a + b?


It is critical to understand that in one case addTogether returns a number, in one case it returns a function, and in all other cases it returns undefined.

1 Like

Remember, addTogether is returning a function in this particular case. This function takes one parameter, x. When you call it you pass in the value you want to use for x. So in my example above (repeated again for simplicity):

const returnFunc = addTogether(5);
const sum = returnFunc(7);

We are passing in the number 7 when calling the returned function. So when that function executes, x will be set to 7 because that’s what we passed into it.

It works just like any other function. Let’s take the following function:

function twiceAsBig(x) {
  return x * 2;
}

If you called this function as

const doubled = twiceAsBig(10);

Then I think you would find it obvious that the value of x inside the function is 10 because we passed 10 into the function.

It’s working the same way with the returned function from addTogether.

1 Like

I made a rudimentary code below:

function addTogether() {

const [a,b] = arguments;

return function(b) {

return a+b

}

}

Does this code work because the function addTogether(5)(7) is being read as addTogether(a) with the remaining parameter being defined as b and ran under the nameless function?

Let’s make a change here. Recycling variable names is making unnecessary confusion.

function addTogether(a, b) {
  return function (c) {
    return a + c;
 }
}

Hopefully, this is a bit clearer, but that’s the basic idea here.


With this, you have one third of the logic. You just need to check if a is a number and if b is a number or defined and add the corresponding logic.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.