Explain ! Intermediate Algorithm Scripting: Arguments Optional

I read the question but not able to understand what is expected,this is my solution

function addTogether(a,b) {
  if(typeof(a) != "number" ||typeof(b) != "number" || a+b == undefined ){
    return undefined;
  }
  else{
    return a+b;
  }
}

Hello there,

Let us make a list:

  1. If a is not a valid number, return undefined
  2. If b is not a valid number, return undefined
  3. If a is a number, but b is not defined, return a function that accepts 1 argument, and returns the sum of a and that argument.
  4. Is a is a number, and b is a number, return the sum of a and b.

Hope this helps

1 Like

Did you read the documentation?

http://forum.freecodecamp.org/t/ -challenge-guide-arguments-optional/14271
They explain in a few easy to understand points what it is about :3

modified the code but still error

function addTogether(a,b) {
  if(Number.isInteger(a))
    if(Number.isInteger(b)){
      return a+b;
    }
    else if(!b){
      return bCheck(b);
    }
      else{
        return undefined;
      }
      }

function bCheck(b){
  if(Number.isInteger(b)){
      return a+b;
      }
}

You need to think about what JavaScript it going to do. That is, if b does not exist (has not been defined), then this block of code will be a problem:

if(Number.isInteger(b)){
      return a+b;
    }
    else if(!b){
      return bCheck(b);
    }

Because, your first check is if b is an integer, then you check if b exists…it makes more sense to check if b exists, then check if it is a valid number. (remember, a and b do not have to be integers…)

Now, this block:

else if(!b){
      return bCheck(b);
    }
      else{
        return undefined;
      }

You are returning bCheck(b) if b does not exist, and undefined if b does exist…?

:frowning:

not working for addTogether(2)(3),
I dont get it how this fun calling is correct because argument is in brackets , it should be ((2),(3)) ?
preach !!

function addTogether(a,b) {
  console.log(b);

  if(Number.isInteger(a)){
    if(!b){
      return bCheck(b);
    }
  else if(Number.isInteger(b)){
      return a+b;
    }
    else{
      return undefined;
    } 

  }

}

function bCheck(b){
    console.log(b);
  if(Number.isInteger(b)){
      return a+b;
      }
      else{
      return undefined;  
      }
}

addTogether(2)(3);

It is a slightly confusing syntax, but think about it like this: One of the points

Here is an example:

// myFunc returns a function
function myFunc() {
  return add(b)
}
// This is the function returned by myFunc
function addOne(num) {
  return num + 1
}

console.log(myFunc()(10)); //This returns 11

Why??

  • myFunc is called. It returns a function.
  • The returned function (addOne) takes one argument.
  • I chose to pass 10.

It is basically this:

// Call the function
myFunc()
// myFunc returns addOne. So, is the same as
addOne
// Now, use the function
addOne(10)

Hope this helps

Tell us what’s happening:
whats wrong with code its not working for
addTogether(2)(3)
addTogether(2)([3])


function addTogether(a,b) {

if(typeof(a) == "number"){
if(!b){
bCheck(b);
}
else{
if(Number.isInteger(b)){
 return a+b;
}
else{
 return undefined;
}
}
}
else{
return undefined;
}
}

function bCheck(x){
if(Number.isInteger(x)){
 return a+x;
 }
 else{
 return undefined;  
 }
}
addTogether(2)(3)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.123 Safari/537.36.

Challenge: Arguments Optional

Link to the challenge:

Because addTogether doesn’t return a function. You should call it with two arguments like
addTogether(2, 3)

its not me its a test case
addTogether(2)(3)

Don’t you already have a thread on this topic? In any case, I’m trying to look at your code.

Here is an easier to read version of your code.

function addTogether(a,b) {
  if (typeof(a) == "number") {
    if(!b) {
      return bCheck(b);
    } else {
      if(Number.isInteger(b)) {
        return a+b;
      } else {
        return undefined;
      }
    }
  } else {
    return undefined;
  }
}

function bCheck(x){
  if (Number.isInteger(x)){
    return a+x;
  } else {
    return undefined;  
  }
}
addTogether(2)(3)

@nibble I removed bCheck() and placed it with a return condition as anonymous function , it worked but why ?

if(!b){
    return function(x){
  if(Number.isInteger(x)){
      return a+x;
      }
      else{
      return undefined;  
      }
};

My point exactly. The problem requires you to return a function when one argument is passed. You haven’t implemented that in your solution therefore you can not do this
addTogether(2)(3). If you do it will throw this error
TypeError: addTogether(…) is not a function.

1 Like

Your problem is this part

if (!b) {
  bCheck(b);
}
  1. You aren’t returning anything.
  2. If you just add a return, you aren’t returning the right thing. You would be returning the value of the function call bCheck(b) rather than returning a function that evaluates a + b.