Arguments Optional assistance

function addTogether() {
  
  if (typeof(arguments[0]) !== "number" || typeof(arguments[1]) !== "number") {
    return undefined;
  }
  
  if (arguments.length > 1) {
    return arguments[0] + arguments[1];
  } 
  else {
    //function here?
    
    
  }  
}

addTogether(2)(3);

My code passes for the 1st, 3rd & 4th tests but I’m unsure of where to go from here. For the next test of addTogether(2)(3) I’m guessing that under my else statement I have to write another function that takes 3 as a parameter and adds it to 2 which was input in the original function? Is this right?

I’m running into trouble if this is correct as I can’t use the arguments object to access (2)(3) I get an error saying TypeError: addTogether(…) is not a function. Where would I take it from here?

If there is one argument, you should return function.

You might want to turn arguments object into array - then it will be more simple to manipulate it’s data.

This was a tricky problem … i felt we did nothing to prepare us for this and understand why people are confused by it so im going to give some help
here is a link … its not doing the problem for you but its doing a section that i think you havent a understanding of. And with what you have and this you might be able to figure it out … or else youll have further questions.

link would not work properly when you click it … so copy whats between the brackets and paste into a new tab and it will work

[http://www.pythontutor.com/visualize.html#code=var%20add%20%3D%20function%20(x%29%20%7B%0A%20%20return%20function(y%29%20%7B%0A%20%20%20%20return%20x%2By%3B%0A%20%20%7D%3B%0A%7D%3B%0A%0Aconsole.log(add(2%29(3%29%29%3B%0Avar%20sum%20%3Dadd(2%29%3B%0Aconsole.log(sum(3%29%29%3B&cumulative=false&heapPrimitives=false&mode=edit&origin=opt-frontend.js&py=js&rawInputLstJSON=%5B%5D&textReferences=false]

I’ve considered this, though I’m not sure how to go about doing that. In this exercise if I have the arguments addTogether(2)(3); for example I get the error addTogether is not a function. This happens before any code runs, so I’m not sure how I’d even go about accessing the arguments object.

BTW for reference I have worked out the following code now, however the addTogether is not a function error is still messing things up.

function addTogether() {
  
  if (typeof(arguments[0]) !== "number" || typeof(arguments[1]) !== "number") {
    return undefined;
  }
  
  if (arguments.length > 1) {
    return arguments[0] + arguments[1];
  } 
  else {
    var addOneMore = function(secondArg) {
      return addTogether(arguments[0], secondArg);
    };
    return addOneMore;
  } 
}
addTogether(2)(3);

when you are doing addTogether(2)(3) you are always going to fail here and return undefinded and then get … addToghether is not a function error … as you are checking for two arguments and only sending one it fails and returns undefined … as addToghether(2) is only one arg … the (3) is not sent initially but is a parameter for the returned function (which you haven’t created yet) and is a self invoked function call with 3 as a parameter.

Did you check out my post … it shows how to return the function you need to create and its called by the second ()

you need to check initially how many parameters are sent … if 2 are sent (2,3) you return x+y … if one parameter is sent (2)(3) … looks like two but is only one … you need to return a function that takes a y parameter eg 3 then this function is run and it adds x which is 2 and y which is 3 … other than that you just need to check if the parameters sent are numbers.

look at my previous post it will help you

2 Likes

I apologize, when I first read your message the link you included wasn’t working, but it does now. Thank you for explaining why my original check for undefined was failing, that makes a lot more sense now. I was having trouble understand that only 1 of those arguments was going into the function at a time, and therefore the second argument I was expecting to include wasn’t there. Let me have a further look at this and see if I can get any farther.

I really appreciate your assistance on this :slight_smile: On a side note I’m thinking recursion might be a bit of a sticking point for me. If you know any outside resources that can help me practice and get this concept down I’m all ears.