Arguments Optional - Missing the testcase with an array inside

Hello everyone.
I wrote the following code to solve the challenge Arguments Optional and it fails on the testcase with the array inside it. I really have no idea why, because it absolutely checks if all arguments are a number! As I misread the instructions slightly, I wrote the function so that it should cope with any number of arguments, not just two - but that seems not to be the problem here.


function addTogether() {
  const args = [].slice.call(arguments);
  console.log("INPUT: " + args);
  if (args.length > 1) {
    if (args.every(val => typeof(val) === "number")) {
      return args.reduce((sum, val) => sum += val);
    } else {
      return undefined;
    }
  } else if (args.length === 1) {
    if (typeof(args[0]) === "number") {
      return function(x) {
        return x + args[0];
      }
    } else {
      return undefined;
    }
  }

  return false;
}

addTogether(2,3);

Any idea would be very appreciated, thank you in advance.

Hi

Your code checks that the arguments passed to addTogether() are valid numbers.

But if addTogether() is only passed one argument, the second argument will be passed to the function that addTogether() returns.

Does that function check for valid numbers?

1 Like

Hi Kitty Mac,
thank you very much for your hint! After adding a number check to the returned function, it works like a charm. Would never have found this problem, as I am still very new to callbacks.
Thanks again.

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