Python noob
Python noob

Reputation: 173

Why wont my closure function work?

The function below adds arguments within 1 paranthesis fine. For example, it computes addTogether(2,3) = 5 and addTogether(2,"3") = undefined.

However, it fails to compute addTogether(2)(3) = 5, instead giving me the error that "addTogether(...) is not a function". The closure function (return function(x)) is supposed to take into the second argument in addTogether(2)(3), and I'm lost on why it dos not work.

function addTogether() {

  if (typeof arguments[0] !== "number" || typeof arguments[1] !== "number") {
    return undefined;
  } //not harmful but not necessary
  var sum = 0;
  var num = arguments[0];

  if (arguments.length === 1) {
    //if only 1 argument in the original function...
    if (typeof arguments[0] !== "number") {
      return undefined;
      //returns undefined if arguments[0] isnt a number
    }

    return function(x) {
      if (typeof arguments[0] !== "number") {
        return undefined;
        //in the closure/2nd function, if first argument isnt a number then no sum will be provided
      } else {
        sum = num + x; //x = second given argument
        return sum;
      }
    };

  }

  if (typeof arguments[0] === "number" && typeof arguments[1] === "number") {

    for (var x = 0; x < arguments.length; x++) {
      if (typeof arguments[x] === "number") {
        sum += arguments[x];
        //add the argument[0] and [1] if both are number types, not string or array types or any other types
      } else {
        sum = undefined;
      }
    }

    return sum;

  }
  // the above "if" statement is rsponsible for achieving addTogether(2,3) = 5;

}

console.log(addTogether(2)(3));

Upvotes: 0

Views: 96

Answers (2)

Suren Srapyan
Suren Srapyan

Reputation: 68655

If you want your function to work like addTogether(2)(3), this means that your addTogether must take an parameter and return a function. addTogether(2) this call will return a new function, and then call the returned function with the second parameter.

In your case when you compare

if (typeof arguments[0] !== "number" || typeof arguments[1] !== "number") 

and call the function with one argument, the second typeof arguments[1] !== "number" returns you true, because the second parameter is undefined, so it is not a number and your function returns undefined.

And in your code you can remove some conditions also. Because the above condition will already check them.

function addTogether() {

  if (typeof arguments[0] !== "number") { 
    return undefined;
  } 
  var sum = 0;
  var num = arguments[0];

  if (arguments.length === 1) {  

    return function(x) {
      if (typeof arguments[0] !== "number") {
        return undefined;
        
      } else {
        sum = num + x; 
        return sum;
      }
    };

  }

  if (typeof arguments[0] === "number" && typeof arguments[1] === "number") {

    for (var x = 0; x < arguments.length; x++) {
      if (typeof arguments[x] === "number") {
        sum += arguments[x];
        
      } else {
        sum = undefined;
      }
    }

    return sum;

  }
  
}

console.log(addTogether(2)(3));

Upvotes: 1

Sebastian Simon
Sebastian Simon

Reputation: 19485

if (typeof arguments[0] !== "number" || typeof arguments[1] !== "number") already causes addTogether(2) to return undefined.

Placing that if statement at the end of the function or turning that || into an && fixes it.

Upvotes: 1

Related Questions