Intermediate Algorithm Scripting - Arguments Optional

Tell us what’s happening:

Describe your issue in detail here.

I don’t understand how the function call addTogether(5)(7) works. I can not find any documentation for such function calls on the internet. The only tests that my code currently fails is this call and the call addTogether(2)([3]).

I would prefer to keep my code general and not limited to a predetermined number of parameters and I would prefer not to copy the solutions provided and move on.

Please explain or point me to documentation to this style of function call. Thank you.

Your code so far

function addTogether() {
   for(let index = 0; index < arguments.length; index++){
     console.log(arguments[index]);
     if (typeof(arguments[index]) !== "number"){
       return undefined;
     };//end if statement
   };// end for loop
   
   let sum = 0;

   if (arguments.length > 1){
     for (let index=0; index < arguments.length; index++){
       sum += arguments[index];
     } 
   } else {
     let secondAddend = 0;
     let firstAddend = arguments[0];
     sum = addTogether(secondAddend, firstAddend);
     console.log("the sum is " + sum);
     return sum;}

   function getMoreArguments(){
     addTogether(second);
   }
  return sum;
}

addTogether(2,3);
console.log(addTogether(5))
//console.log(addTogether(5)(7));
console.log(addTogether(5, undefined));
console.log(addTogether(5, undefined));
console.log(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/119.0.0.0 Safari/537.36

Challenge Information:

Intermediate Algorithm Scripting - Arguments Optional

If only one argument is provided, then return a function that expects one argument and returns the sum.

You aren’t returning a function anywhere.

Note - ; don’t go after { } of a loop or if statement

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.