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
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?
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.
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
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.
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.
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.
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?