How is this not an infinite loop?

Assuming only one argument is passed in, it will enter the second if statement. But second is still undefined, so we return the function addTogether with second passed in as undefined enetering the second if statement and so on forever.

function addTogether() {
  const [first, second] = arguments;
  if (typeof(first) !== "number")
    return undefined;
  if (second === undefined)
    return (second) => addTogether(first, second);
  if (typeof(second) !== "number")
    return undefined;
  return first + second;
}

This isn’t passing second into the function. In this case, second is the name of the argument for the function. When you define a function with parameters you give the parameters names so you can use their values in the function body:

function myFunc(argA) {  ... }

argA assumes the value of whatever we provide when we call the function:

myFunc(10);

Inside the function body argA will be 10. This is the same thing that is happening when you return a function. You are just using the same name for the parameter as you are for one of the original arguments, but they are not the same thing.

If this is still confusing then use a different name for the parameter in the function you are returning.

Note that this is basically the same as the other provided solution but this one returns an anonymous function while the other creates the function before returning it.

So like this… Except how does it know that x == 7 once inside second if statement.

function addTogether() {
  const [first, second] = arguments;
  if (typeof(first) !== "number")
    return undefined;
  if (second === undefined)
    return (x) => addTogether(first, x);
  if (typeof(second) !== "number")
    return undefined;
  return first + second;
}

console.log(addTogether(5)(7)); 

Yes, just like that. x will be 7 in that console.log because you are passing 7 into the returned function. Split that console.log up:

const returnedFunc = addTogether(5);

Now returnedFunc is set to the returned function and you can call it just like any other function:

returnedFunc(7);

We just passed 7 into the function and so in this case x will equal 7.

1 Like

So in this longer version:

function addTogether() {
  const [first, second] = arguments;
  // First argument is not a number
  if (typeof(first) !== "number") {
    return undefined;
  }
  // First argument is a number
  //  and second argument is not defined
  else if (second === undefined) {
    function addSecond(second) {
      // New argument is not a number
      if (typeof(second) !== "number") {
        return undefined;
      }
      // New argument is a number
      else {
        return first + second;
      }
    }
    // Note: returning a *function*
    return addSecond;
  }
  // First argument is a number
  //  and second argument is not a number
  else if (typeof(second) !== "number") {
    return undefined;
  }
  // First argument is a number
  //  and second argument is a number
  else {
    return first + second;
  }
}

it enters the second if statement, and defines addSecond function and when the definition is complete it is called. But it still feels like it should be return addSecond(7);

The instructions say to return a function. addSecond(7) is just a number.

No, it is returned. The first else if statement returns a function.

How would you know that the argument is going to be 7? You are returning a function that takes one argument. You don’t know the value of that argument until it is called.

This is a two step process:

  1. Call addTogether with one number. This returns a function that takes one number and then adds it to the number passed into addTogether.
  2. Call the returned function, passing in the second number.

Do you see how you don’t know what the second number is going to be until the function returned by addTogether is actually called and a number is passed into it?

1 Like

I suppose but now it looks like addSecond is only returned and never called.

Which is the instructions. If one argument is provided and that argument is a number, then you need to return a function. This function can later be called after it has been returned.

1 Like

addTogether(5)(7)

This is calling the addSecond function. Look again a few posts above where I split this into two parts. Granted, we are never actually using the name addSecond to call it, but that name is just a convenience for you, you don’t have to define the returned function with a name. For example, in the original code you posted you were returning an anonymous function.

1 Like

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