Help with Arguments Optional, please

Help with Arguments Optional, please
0

#1

Through some small amount of ingenuity and a bit of outright theft I have been able to cobble together some functions that nearly solve this problem. Unfortunately, I’m also stuck.

I’ve been staring at this code long enough that it’s just turning into a muddy mess in my head.

Any help would be appreciated; I am passing all of the tests except for the second one: “addTogether(2)(3) should return 5”.

Here’s my code:

function addTogether() {	
//CHECK IF IT BE A NUMBER
function isANum(y){
    if (typeof y === "number"){
        return true;
    }else{
        return false;
    }
}
//IF THERES 1 ARG TRY TO ADD THE NEXT
if(arguments.length < 2){
  if(isANum(arguments[0])){
    return function(x){
      if (!isANum(x)) {
        return undefined;
      }else{
        return arguments[0] + x;
      }
    };
  }
//IF BOTH ARE NUMBERS, ADD AS NORMAL
}else if(isANum(arguments[0]) && isANum(arguments[1])){
  return arguments[0] + arguments[1];

//IF THERES JUST THE ONE ARGUMENT AND IT IS A NUMBER
}else{
  return undefined;
}
}

addTogether(2,3);

Just to be clear, I’m looking for an explanation of what is missing or how this should work. I know I’m dropping the second number somewhere but, i can’t see it right now.

Thanks in advance.


#2

Hi,
I like your code, I did some stealing myself where you are stuck. I think you’re having some difficulty because you keep using arguments every time, How about you put all arguments into one array first
var parameters = [].slice.call(arguments);

then now coz you have an array, you can simply check if every element in the array is a number
var x = parameters.every(function(val) {return typeof val === “number”}); //x will be either true if all elements are numbers and false otherwise

if(x === false) {return undefined;} that way you will never have to confirm if both parameters are numbers again.

Now because all parameters are in one array
if(params.length === 2) {return params[0] + params[1]} //this part you already solved

so to the tough part
else{
//use the one parameter you have to recall the function
var param1 = parameters[0];
var addParam2 = function(param2) {return addTogether(param1, param2);}

return addParam2;
}

basically what this does is it runs addTogether(2) which will return 2, then afterwards run addTogether(3) which means it will now return 2 + 3 = 5. So basically the function will call itself a second time with 3 as the argument. And where 3 is not a number now it will simply return undefined.
It’s a bit confusing for me too but that is what closures do, they allow you to call a function again within the function and attach it to a variable. I found this part online, apparently it’s a common question in interviews.

Sorry I had to start from scratch, I have a hard time understanding people’s code.


#3

You also might want to read about spread operator. This is ES6, but it lets you easily make arguments into array.


#4

That’s exactly what I needed! Thanks so much, man. I had forgotten the recursive bit and I think that’s what did it for me.

Do you have a reason why I should need to plot the arguments into an array instead of just working with them directly?

I followed your advice on it but, I don’t really see the logic behind it.

Thanks again.


#5

Hey
Glad you found my advice helpful.

The reason you put them in an array is because it makes it easier to work with.

For example, notice how you kept checking if the arguments were numbers twice, whereas I simply put them all in an array and used Array.every().
There are times you will have trouble accessing arguments or manipulating them efficiently, and since working with arrays is much easier, then it’s better to just put the arguments in one. It gives you more control over arguments.
Basically it makes understanding the code as simple as possible for everyone else, and even more importantly, for yourself.

Imagine if you had to filter the arguments for some reason, you would have alot of trouble coz arguments is an object, not an array. So making it into an array gives you an already made array.filter() function.

So whenever you find yourself having a hard time manipulating arguments or repeating it constantly, just put them in an array.