Returning a function doesn't seem to work as expected

Tell us what’s happening:
I have been working on the intermediate algorithm scripting challenge called “arguments optional”.

From my debugging efforts so far it looks like the problem is in returning a function correctly in the case that argument 2 is not given.

I have tested the function and it does exactly what it is supposed to do.

However, when I return that function in my code, it fails the tests.

I have checked and searched everything. I seem to be doing things exactly the same way as solution 1. I do not understand at all why my code is not working.

Can anybody point me into the right direction?

  **Your code so far**

function addTogether(num1, num2) {

function checkNum(num){
  if(typeof(num) !== "number"){
    return undefined;
    } else {
      return num;
    }
}

function adds(add2){
  if(checkNum(add2)){
    return num1+add2;
    } else {
      return undefined;
      }
    }

if(!checkNum(num1)||!checkNum(num2)){
  return undefined;
} else if(checkNum(num1)&&checkNum(num2)){
  return num1+num2;
} else if(num2==undefined){
    return adds(add2);
    }
}

addTogether(2,3);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0.

Challenge: Arguments Optional

Link to the challenge:

1 Like

Yes, the issue is with a test of the following type:

addTogether(5)(7) should return 12.

Or to be more precise, the issue is this (See the explanation of the task):

addTogether(2) should return a function.

There is two simple issues with your logic, that prevent this from happening.
You could try to log the above to the console and I’m sure you’ll be able to figure out a way to solve the issue. If not, just let me know again :slight_smile:

Thanks a lot for your reply! I appreciate you trying to help me solve it on my own. That’s how I prefer to learn, whenever possible.

I already tried logging addTogether(5)(7) to my console. And it only had me more confused, because it told me that addTogether is not a function when it clearly is defined as a function.

And the fact that addTogether(2) returns a syntax error just confirms that I don’t seem to understand how to correctly return that function, but I really cannot see the difference between my code and the proposed solution.

I have initially returned the function as is and not as defined it earlier. Which however, should not make a difference. And it indeed doesn’t. (as shown below) I cannot think of anything else I could try to change.

function addTogether(num1, num2) {

function checkNum(num){

if(typeof(num) !== "number"){

  return undefined;

  } else {

    return num;

  }

}

if(!checkNum(num1)||!checkNum(num2)){

return undefined;

} else if(checkNum(num1)&&checkNum(num2)){

return num1+num2;

} else if(num2==undefined){

  return function (add2){

    if(checkNum(add2)){

      return num1+add2;

      } else {

        return undefined;

        }

    }

}

}

addTogether is a function, but the error message says something else :wink: it says, that addTogether(…) is not a function.
Also:
console.log(addTogether(2)) //This should return a function, but instead, it returns undefined

This is because your code never reaches your last else if statement. In fact, it can’t.
Because if num2 is undefined, it will return undefined and not a function. (This part you should be able to figure out)

Now as far as returning a function goes understand this:
(Note: I don’t know how accurate the following actually is, but after more than a year of active JavaScript it hasn’t hurt me so far)
A function is basically something like a template. It will handle input and give output (which is just the input that went through the conditions that are set up).

On the other hand a function that is passed input, can be thought of as a value, not a pattern.

Example:

Addition. Addition is a function, it goes like this:

function addition(a,b) {
    return a+b
}

So basically whenever we write ‘+’, what we mean by that is a function (meaning not just in javascript, but in all areas). But on the other hand, 4+5 is not really a function, it’s a value. it’s equal to 9. which is not a function.

So with all of that hopefully I was able not only to give you the following answer, but also give an understanding of why it makes sense:

When returning a function, just return it’s name like this:
return adds

(again back to the analogy, if I asked you to return the function of addition, you wouldn’t give me an example, like 4+5, but instead just the +)

1 Like

Oh no! I am glad it was such a ridiculous mistake (and equally annoyed by it…).

Of course, the initial OR statement is the problem.

So I wasn’t completely wrong in believing my code (in regards to how I wrote the function to be returned) should work :smiley:

Thanks a lot for your patience, and for patiently letting me figure that out by myself :slight_smile:

You just made my evening and I can finally go to bed :joy: :sweat_smile:

This code works :wink:

function addTogether(num1, num2) {

function checkNum(num){

if(typeof(num) !== "number"){

  return undefined;

  } else {

    return num;

  }

}

if(!checkNum(num1)){

return undefined;

} else if(checkNum(num1)&&checkNum(num2)){

return num1+num2;

} else if(num2==undefined){

  return function (add2){

    if(checkNum(add2)){

      return num1+add2;

      } else {

        return undefined;

        }

    }

}

}

1 Like

One more thing: While your code does pass all the tests, there is one minor inaccuracy in terms of the intended way it should work:
try this:

console.log(addTogether(2)(0));

This will return undefined, and not 0. The reason is quite simple:
console.log(0 == false) //this will return true because 0 is a falsy

(Also make sure to read the edited post above, because this:

So I wasn’t completely wrong in believing my code (in regards to how I wrote the function to be returned) should work :smiley:

is actually not accurate and I wouldn’t want that to lead to future confusion.

And also I might add, that the initial code looks a bit nicer :wink: )

So I fixed the obvious oversight of 0 being a falsy value by changing the checkNum function to return true instead of num. But I managed to break my code elsewhere again.

I will make use of my long aquired coding wisdom which says the later the hour the more plentiful the bugs, thus do not code after 2 am. I will return to it tomorrow.

Thanks for your help.

1 Like

Hm, I just tried to replace num with true and if that’s all you do, it should actually solve the issue and still work. (I did this in the original code)

Also please do let me know, if anything of my function explanation was unclear.

1 Like

True when just replacing num with true, it works fine. So I am leaving it like this now.

I had been putting the function to be returned into a separate block of code again in attempt to make it look neater again. But somehow that is giving problems.

Anyways, it works with just replacing num with true indeed, so I am leaving it as is.

Thanks a lot once again for all your help.

1 Like

Congrats btw on figuring it out! I’ll just post the solution with the function in a different block here (it’s the same as the original one except:

  1. Replacing num with true
  2. replacing || with &&
  3. replacing adds(add2) with adds
    )
function addTogether(num1, num2) {

function checkNum(num){
  if(typeof(num) !== "number"){
    return undefined;
    } else {
      return true;
    }
}

function adds(add2){
  if(checkNum(add2)){
    return num1+add2;
    } else {
      return undefined;
      }
    }

if(!checkNum(num1)&&!checkNum(num2)){
  return undefined;
} else if(checkNum(num1)&&checkNum(num2)){
  return num1+num2;
} else if(num2==undefined){
    return adds;
    }
}

addTogether(2,3);

1 Like

I added spoiler tags to your post. For what it’s worth, the guide post for this challenge also has some pretty good solutions that I think are worth looking at.

2 Likes

Yes, that works. As you mentioned earlier when referring to the function to be returned, I should just have used the function name, but instead I left the syntax like this:
return adds(add2) , which caused the issue.

Thanks again for your help!

1 Like