Arguments Optional: Recursive function takes 2nd input

Hi! I found this solution to the Arguments Optional intermediate javascript challenge. Can someone please explain to me what’s happening in the first if statement? How does this recursive function actually take a second input? I don’t understand why the second input is passed to the anonymous function as “x” while “a” is already coded into the return statement…

  **Your code so far**

function addTogether() {
  var len = arguments.length;
  var a = arguments[0];
  var b = arguments[1];
  var isNum = function(arg) { return Number.isFinite(arg); };
  if (len === 1 && isNum(a)) {
    return function(x) {
      if (isNum(x)) {
        return a + x;
      }
    };
  }
  else if (len === 2 && isNum(a) && isNum(b)) {
    return a + b;
  }
}

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36.

Challenge: Arguments Optional

Link to the challenge:

There is no recursion.

The sort of funny thing going on is that a function is an object in JavaScript, and any object can be saved to a variable or returned from a function.

In this case, a now function is returned, and this function takes an argument, x, and return the sum of x and a if x is a number.

For what it’s worth, the variable names a, b, and x are not great.

function addTogether() {
  // addTogether arguments
  let len = arguments.length;
  let first = arguments[0];
  let second = arguments[1];

  // Argument checking helper
  function isNum(arg) {
    return Number.isFinite(arg);
  };

  if (len === 1 && isNum(first)) {
    // Create new function 
    function addThis(value) {
      if (isNum(value)) {
        return first + value;
      }
    };
    // return this function
    return addThis;
  }
  else if (len === 2 && isNum(first) && isNum(second)) {
    return first + second;
  }
}

I have rewritten this with slightly different syntax.

This function never calls itself. This function creates and returns a new function.

1 Like

Now the question is, did @sofiyavolobuyeva understand your answer to her question :slight_smile:

2 Likes

Thank you for explaining! I see now that it isn’t recursion. However I’m still stuck on the crux of this solution. What is actually passed to addThis? From my understanding, the second input passes to the function and then is added to “a” within the function. But why is “a” coded into the function while the second input is passed as (value)? The challenge hint says not to worry about prompting the second input, and maybe that’s why I feel like there’s a gap in my understanding.

Nothing is passed to addThis inside of this function call to addTogether (but every function does have a ‘closure’, which I’ll explain below)

In JavaScript, you can create a function inside of a function. You can return a function from a function for later use.

So in this case, we could

const addThree = addTogether(3);

// some time later
console.log(addThree(5)); // should log 8

In JavaScript, functions actually keep track of all the variables they need to run. So in this case, the addThis function keeps track of the 3 that was passed into the addTogether function.

We save the addThis function that was returned from this call to addTogether into the addThree variable, and when we call addThree later, this function will use the argument from the first call to addTogether, which was 3. (Keeping track of and later using this variable is what’s know as ‘closure’)


This can be a bit tricky to wrap your head around. It took me a while to understand at first, so let me know if there are parts I can explain better!

1 Like