Arguments Optional - My code works, but fails the test

Tell us what’s happening:
My code works, but fails the test.

Your code so far


function addTogether() {
  let result = 'v';
  for (let item of arguments) {
    if (typeof item !== 'number') {
      result = void(0);
    }
  }
  if (result === 'v') {
    if (arguments.length === 2) {
      result = arguments[0] + arguments[1];
    } else {
      let y = arguments[0];
      result = function(x) {
        return y + x;
      }
    }
  }
  return result;
}

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/75.0.3770.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/arguments-optional

You need to check the type on the second argument as well.

addTogether(2)([3])
// returns "23"

2 + [3]
// returns "23"

Otherwise, if it’s a string or something that will coerce to a string, you will end up doing string concatenating and will be returning a string.

1 Like