freeCodeCamp Challenge Guide: Arguments Optional

@danedavid danedavid
have you figured it out yet?

am keen to get some feedback on my solution:

function addTogether(a,b) {
  if(![...arguments].every(arg => typeof arg === "number")){
    return
  }
  if(b){
    return a + b
  } else {
      return function(y, x = a){
        return addTogether(y, x)
      }
    }
}
4 Likes

@ashok-cs

FYI, Have a look at the […Spread Operator ] (Spread syntax (...) - JavaScript | MDN) instead of this piece of code

@TeeJay
try running this in your browser to further understand:

function outerFunction(a){
  console.log(`${a}, this is from the outer function`)
  
  // outerFunction returns another *anonymous/unnamed* function here, which we call with the ("Gday") 
  return function(x){
    console.log(`${a}, this is from the middle function`)
    console.log(`${x}, this is from the middle function`)
    // middle function returns another *anonymous/unnamed* function here, which we call with the ("Gday") 
    return function(y){
      console.log(`${a}, this is from the inner function`)
      console.log(`${x}, this is from the inner function`)
      // y (which is "Goodbye" as we passed the argument below) is the final return which  
      return y
    }
  }
}


console.log(
  outerFunction("Hello")("Gday")("Goodbye")
)

As you can see, the arguments passed to the outer functions are all accessible by the inner/callback functions

7 Likes

not really, somehow, I passed the test… But still I dont knw wat’s wrong with the above code…

try removing typeof undeclared

typeof returns a string, so you were returning a string saying “undefined” rather than actual undefined ```

Thanks! And now I know how I got it right earlier…
I did
return und;
instead of your
return

Hey good point. addTogether(“string”) returns undefined, so you’re essentially telling the computer to call undefined(3), so it’s just saying undefined isn’t a function.
You can address that by having addTogether return a function which returns undefined rather than just returning undefined. Here’s my answer with this taken into account:

function addTogether() {
  var args = Array.prototype.slice.call(arguments);
  if (args.every(function(a){return typeof a === 'number'})) {
  
  if (arguments.length > 1) {
    return arguments[0] + arguments[1];
  } else if (arguments.length === 1) {
    var b = arguments[0];
    return function(a) {
      if (typeof a !== 'number' || typeof b !== 'number') {
        return undefined;
      }
      return b + a;
    };
  }
  } else {return function(){return undefined};}
}

addTogether('string')(3);
1 Like

never mind, now it doesn’t pass the other tests lol, let me think on this one…

function addTogether() {
  var firstArgument = arguments[0];
  if (typeof firstArgument != 'number' || arguments[1] && typeof arguments[1] != 'number' )
    return undefined;
  return arguments.length == 2? (firstArgument + arguments[1]) : function(x) {
    return (typeof firstArgument == 'number' && typeof x == 'number')? firstArgument + x : undefined;
  };
}
function addTogether() {
  var arg1 = arguments[0];
  var arg2 = arguments[1];
  
  provideArg = function(arg){
    return (typeof arg == typeof arg1) ? arg + arg1 : undefined;
  };
  
  var flag = typeof arg1 == typeof arg2;
  if(arguments.length>=2 && flag ===true) return arg1 + arg2;
  else if (arguments.length===1 && typeof arg1 != 'number') return undefined;
  else if(arguments.length===1) return provideArg;
  else return undefined;
  
}

here’s my solution :smiley:

function addTogether() {
  var args = Array.from(arguments)
  for(let i=0; i<args.length; i++){
    if(typeof args[i]!=="number"){
      return undefined;
    }
  }
  if(args.length > 1){
    if(args.every(num=>typeof num === "number")){
     return args.reduce((acc, next)=>acc+next,0)
    }else{
      return undefined
    }
  }
  return function(arg2){
    for(let i = 0; i<args.length; i++){
        if(typeof args[i] === "number" && typeof arg2 === "number"){
    return args[i] + arg2 
          
        }else{
          return undefined
        }
    }
  }
}

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