Intermediate Algorithm Scripting - Arguments Optional

Tell us what’s happening:
The given answers are understandable. I tried to pass ‘first’ as an argument also into construction of the function that is to be returned. This is however not working. Unable to understand why this is not working. Thanks in advance for any explanation :slight_smile:

Your code so far

function addTogether(...args) {
  const [first, second] = args;
  if (args.length === 1 && typeof first === 'number') {
    let tempFunction = (num,first) => {
      if (typeof num === 'number') {
        return first + num;
      }else{
        return undefined;
      }
    };
    return tempFunction;
  }
  if (typeof first === 'number' && typeof second === 'number') {
    return first + second;
  }
}

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Arguments Optional

Link to the challenge:

Addition, just ‘num’ in temoFunction works! So the problem seems to be with adding ‘first’ to argument list!!

Part of the instructions state:

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

The function you return (see below) is designed to take two arguments instead of just one, so when a call like addTogether(5)(7) occurs, only num in the return function receives a value. The first parameter will always be undefined which will cause you to end up returning NaN if num is a number because 7 + undefined evaluates to NaN.

The function you return still has access to the first variable in the main function addTogether, so if you had to choose only one parameter for the returned function, which one would it be. Make this one small (yet critical) change and you will pass all the tests without having to change any other code.

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