Intermediate Algorithm Scripting - Arguments Optional

Tell us what’s happening:
Describe your issue in detail here.

It’s passing everything but the “undefined” tests, and I’m not sure why. The condition is there and it’s in the beginning of the conditions, so I would assume it would catch all undefined elements first…

Your code so far

function addTogether() {

  // make a copy of the arguments passed in using the "arguments" object
  let args = [...arguments];

  // loop through the args array using a "for" loop
  for (let i = 0; i < args.length; i++) {
    // create a conditional chain, starting with returning "undefined" if none of the arguments are numbers
    if (typeof args[i] !== 'number') {
      return undefined;
    } else if (args.length === 1) {
      // if there is only one argument in the arguments list, return a function that takes in a number and calls the addTogether function on that passed in number and the element positioned at args[0] (the only element in the array)
      return function addNum(num) {
        return addTogether(args[0], num);
      }
    } else {
      // otherwise, return the addition of the two indices of args
      return args[0] + args[1];
    }
  }
}

addTogether(2,3);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0

Challenge: Intermediate Algorithm Scripting - Arguments Optional

Link to the challenge:

Your return statements inside of a loop are tripping you up again.

return immediately halts the function and no more loop iterations run.

I fixed it. I needed to take the last two conditions of the if-else chain and put them outside of the “for” loop. Thanks for the help! I really need to work on that lol.

Good work getting it passing!

This is a tricky challenge. Now that you have a solution, I’d look at some of the other possible answers on the Hints page: freeCodeCamp Challenge Guide: Arguments Optional

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