Arguments Optional - I need closure (pun fully intended, please enjoy)

After much googling , I’m unable to move past this point in my coding trial and error. I’m trying to have one function call another but am obviously failing. Please help.

The console.logs tell me that for testcase, " addTogether(2)(3) should return 5." both num and arguments[0] are 3 instead of what I want which is: num = 3 , arguments[0] = 2.

Your code so far


let sum = 0;

  if(arguments.length === 1 && isNaN(arguments[0]) === false){
    return function (num){
      console.log("This is num: " + num);
      console.log("This is arguments[0]: " + arguments[0]);
      sum = num + arguments[0];
      console.log("This is the sum: " + sum);
    }

  }else if(arguments.length >= 2){
    for(var i = 0; i <= 2; i++){
      if(isNaN(arguments[i]) === false){
        sum += arguments[i];
      }
    }
    return sum;
  }
  else{
    return undefined;
  }
}

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional/

arguments[0] is 3 for the nested function as well because arguments is scoped locally, not globally – this means that arguments to the inner function’s arguments inside that function

1 Like

Thanks @lekha. Saving the outer function’s arg before getting into inner function as a variable fixed the problem.

After fixing that and a few other problems, I ended up with this code (which needs to be cleaned up) but is now failing the first testcase. I put in a bunch of console.logs but the strange this is that the logs for both if(isNaN(arguments[i]) === false) and the corresponding else statements are passing. How is that possible?

// var add = (function () {
//   var counter = 0;
//   return function () {counter += 1; return counter}
// })();

// ---------------------------

function addTogether() {

//1 arg: is it a number?
  //return a func that has 1 parm (which is a number)
  //new func will add 1st arg with own parm

//2 args: are 1st 2 args numbers?
  //Yes, add together and return sum.
  //No, return undefined.

  let sum = 0;

  if(arguments.length === 1 && typeof(arguments[0]) == "number"){
    let firstNum = arguments[0];
    return function (num){
      if(typeof(num) == "number"){
        return num + firstNum;
      }else{
        return undefined;
      }
    }
  }
    else if(arguments.length >= 2){
      console.log("Debug spot # 1");
      for(var i = 0; i <= 2; i++){
        if(isNaN(arguments[i]) === false){
          console.log("This is arguments[i]: " + arguments[i]);
          sum += arguments[i];
        }else{
          console.log("Debug spot # 4");
          return undefined;
        }
      }
      console.log("Debug spot # 2");
      return sum;
    }
    else{
      console.log("Debug spot # 3");
      return undefined;
    }
  }

// // addTogether(2,3);

isNan is a strange function (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN#Description) – try Number.isNaN (see here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN)

3 Likes

what’s wrong with the good’ole typeOf?

1 Like

Thank you @lekha.

Here’s my final code, in case anyone is interested. (I need to work on cleaning up code; this looks so ugly even to me):


function addTogether() {

//1 arg: is it a number?
  //return a func that has 1 parm (which is a number)
  //new func will add 1st arg with own parm

//2 args: are 1st 2 args numbers?
  //Yes, add together and return sum.
  //No, return undefined.

  let sum = 0;

  if(arguments.length === 1 && typeof(arguments[0]) == "number"){
    let firstNum = arguments[0];
    return function (num){
      if(typeof(num) == "number"){
        return num + firstNum;
      }else{
        return undefined;
      }
    }
  }
    else if(arguments.length >= 2){
      for(var i = 0; i < 2; i++){
        if(Number.isNaN(arguments[i]) === false && typeof(arguments[i]) == "number"){
          sum += arguments[i];
        }else{
          return undefined;
        }
      }
      return sum;
    }
    else{
      return undefined;
    }
  }

// // addTogether(2,3);

1 Like