[solved]Help in debug Arguments optional

[solved]Help in debug Arguments optional
0

#1

Hello folks!
Here’s my code, my question is: why this return the entire anonymous function?

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


addTogether(3);

Here the return:

function(y) {
            if(isNaN(y)){
              return undefined;
            }
            else{return arguments[0] + y;}           
       }

Thanks! :slight_smile:


#2

You only have one argument in the function call, so the if-condition fails and runs the code in the else block instead.


#3

Hi Kev! :slight_smile:

runs the code in the else block instead.

Yes, that’s what I expected. But why instead of having the return of the function (y) I have the function(y) itself? :sweat:
I would have expected an undefined or the sum… not the entire function.


#4

It’s because you can pass functions around as values in JS. You can have a function that returns another function (like this one), or you can have functions accept another function as arguments (more commonly known as callbacks).

In this case, you have a function right after the return keyword, so that function’s what you get.


#5

You answered your own question really, it’s because you returned a function. The function addTogether is suppose to return a function if it only received 1 argument, so you wouldn’t have a result until you give it another argument.

you should expect

var addOne=addTogether(1);

to assign a function to addOne, and when you do this

addOne(2);

you should get 3

A couple of oddities.

  1. Why the for loop? you either get 1 argument or 2 arguments in this case, you can easily use an or conditional.

  2. Why is do you use arguments[i] instead of arguments[0] in your return function?

However, the 2nd question is probably irrelevant because you should realize that you are using arguments of different scopes, so they have different values.

In addTogether(3), your arguments is {0:3}, but in your returned function, your argument is {0:y}. Even if you used the condition you meant, your return function wouldn’t work properly. Instead of add 3 to y, it’d add y to y.


#6

Thank you!
I’ve created a function and then call it (still won’t work):

function addTogether() {
  for(var i=0; i<arguments.length; i++){


      if(isNaN(arguments[i])){
          return undefined;
        }
    }
  
  if (arguments.length>=2){
      return arguments[0]+arguments[1];
    }
  else{
       var func= function(y) {
            if(isNaN(y)){
              return undefined;
            }
            else{return arguments[0] + y;}           
       };  
       return func();
  }  
}


addTogether(3);

The onlyfor I have is meant to control if the data is a number or not: I control the arguments in all its length. But it doensn’t always work.
I don’t know why for addTogether(2, "3") the for loop doens’t return an undefined, even if the program treats one argument as a string: I have “23” at the end…

It was a copy/paste mistaping :stuck_out_tongue:
I meant Y, not argument[i], as I’ve written at first. Sorry, that was confusing…


#7

You should return the variable func, instead of func(), but you are still using the wrong argument in func. As I said in the previous post, arguments[0] for func is y,

there is 2 ways to solve this.

You can define a variable in addTogether() and assign arguments[0] to it, and reference that variable in your return function rather than argument[0].

You can also declare addTogether with 1 default argument like

function addTogether(x){
...
    return function(y){
        return x+y;
     }
}

that way you avoid using the wrong argument object all together.


#8

Thank! I solved at the end. I didn’t cacth that argument[0] wasn’t the older one but the new fucntion data!
Ok… first attempt with closures went "so-so"
I hope to get better in their usage soon :smile: