Https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional

Tell us what’s happening:
I am getting the result with console.log, but I am not passing the tests

Your code so far


function addTogether() {

if(Number.isInteger(arguments[0]) && Number.isInteger(arguments[1])){

return arguments[0]+arguments[1]
     
}else if(Number.isInteger(arguments[0])){
                        
      return arguments[0]    

     }
}

function sumTwoAnd(){

  return addTogether(3)+arguments[0] 
}



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

/*Intermediate Algorithm Scripting: Arguments Optional
Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum.

For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function.

Calling this returned function with a single argument will then return the sum:

var sumTwoAnd = addTogether(2);

sumTwoAnd(3) returns 5.

If either argument isn't a valid number, return undefined.

addTogether(2, 3) should return 5.
addTogether(23, 30) should return 53.
addTogether(5)(7) should return 12.
addTogether("http://bit.ly/IqT6zt") should return undefined.
addTogether(2, "3") should return undefined.
addTogether(2)([3]) should return undefined.

*/

Your browser information:

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

Challenge: Arguments Optional

Link to the challenge:

I am not getting the result, I am getting undefined. Your function needs to return a function if is called with only one argument.

1 Like

My teacher, how may I set the second condition? “If only one argument is provided, then return a function that expects one argument and returns the sum.” I already set a function and I do not know how to make it work properly.

function addTogether() {

         if(Number.isInteger(arguments[0]) && Number.isInteger(arguments[1])){

               return arguments[0]+arguments[1]

         }else{

           

            function sumTwoAnd() {

  

  return arguments[0]+addTogether(2)

}

         }  

  

}

let a= addTogether(2,4);

/*

Intermediate Algorithm Scripting: Arguments Optional

Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum.

For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function.

Calling this returned function with a single argument will then return the sum:

var sumTwoAnd = addTogether(2);

sumTwoAnd(3) returns 5.

If either argument isn't a valid number, return undefined.

addTogether(2, 3) should return 5.

addTogether(23, 30) should return 53.

addTogether(5)(7) should return 12.

addTogether("http://bit.ly/IqT6zt") should return undefined.

addTogether(2, "3") should return undefined.

addTogether(2)([3]) should return undefined.

You have a function, addTogether(), which currently takes no arguments. The function needs to take two arguments, as the example in the lesson shows.

Also from the lesson text:
“If only one argument is provided, then return a function that expects one argument and returns the sum.”
The key phrase here is return a function.

I hope this helps point you in the right direction. :slight_smile:

1 Like
function addTogether() {

         if(Number.isInteger(arguments[0]) && Number.isInteger(arguments[1])){

               return arguments[0]+arguments[1]

         }else {

           

            return function(){

                 

       if(Number.isInteger(arguments[0])){

                           

   return arguments[0]+arguments[0]

                  }

            } 

 

         }  

  

}

console.log((addTogether(5)(6)));

addTogether(2)([3]) should return undefined. I only need to complete that test. What may I do?

function addTogether() {

  if(arguments.length===1 && Number.isInteger(arguments[0]) ){

             

              let a=arguments[0]      

            

             return function (b){

                   

                   return a+b

                          

              }     

  }else if(arguments.length===2 && Number.isInteger(arguments[0])&& Number.isInteger(arguments[1])){

               

               return arguments[0]+arguments[1]

  }else{

     return undefined;

  }

  

}

console.log(addTogether(2)([3]));

/*Intermediate Algorithm Scripting: Arguments Optional

Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum.

For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function.

Calling this returned function with a single argument will then return the sum:

var sumTwoAnd = addTogether(2);

sumTwoAnd(3) returns 5.

If either argument isn't a valid number, return undefined.

addTogether(2, 3) should return 5.

addTogether(23, 30) should return 53.

addTogether(5)(7) should return 12.

addTogether("http://bit.ly/IqT6zt") should return undefined.

addTogether(2, "3") should return undefined.

addTogether(2)([3]) should return undefined. */
function addTogether() {

  if(arguments.length===1 && Number.isInteger(arguments[0]) ){

                

     let a=arguments[0]       

             return function c(b){

              

     if(Number.isInteger(b)){

           

             return a + b      

     }

 

   }

                           

  {

              }     

  }else if(arguments.length===2 && Number.isInteger(arguments[0])&& Number.isInteger(arguments[1])){

               

               return arguments[0]+arguments[1]

  }else{

     return undefined;

  }

  

}

console.log(addTogether(3)(1));

You always have a good idea to share. Thanks!