Optional arguments

Tell us what’s happening:
Hi, kindly help me understand why my source code doesn’t return undefined for the given tests.

  **Your code so far**


function addTogether(...args) {
if(typeof args[0] === 'number' && typeof args[1] === 'number') {
  if(args.length === 2) {
      return args[0] + args[1];
  }
  if(args.length === 1) {
      return function(y) {
        return args[0] + y;
      }
  }
} 
return 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/92.0.4515.159 Safari/537.36

Challenge: Arguments Optional

Link to the challenge:

In an example like this:

addTogether(5)(7)

What will it return. Remember that this will first be called as:

addTogether(5)

It will call this first and then expect a returned function that it will call with the (3) part. So, on that first call, it is only getting one parameter. So, that that case, what does typeof args[1] evaluate to? So, what will that first pass through the function return?

1 Like

Thank you for helping.
Below is my source code after rectifying the currying function.


function addTogether(...args) {
    if(args.length === 1 && typeof args[0] === 'number') {
        return function(y) {
          if(typeof y === 'number')
          return args[0] + y;
        }
    }
    if(args.length === 2 && typeof args[0] === 'number' && typeof args[1] === 'number') {
        return args[0] + args[1];
    }    
  return undefined;
}

console.log(addTogether(2,3));   //returns 5
console.log(addTogether(2)(3));  //returns 5

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