Arguments Optional (Console is right, test is wrong)

Arguments Optional (Console is right, test is wrong)
0

#1

The console shows the right number, the sum is exactly 5, but the test shows that it is not right( X ). Anyone can help

Your code so far

  function add(num) {
    sum += num;
    return sum;
  }

  function checkNum(num) {
    if (typeof num === "number") {
      return num;
    } else {
      return undefined;
    }
  }

  if (arguments.length > 1) {
    for (var a in arguments) {
      if (checkNum(arguments[a])) {
        add(arguments[a]);
      } else {
        return undefined;
      }
    }
  } else {
    if (checkNum(arguments[0])) {
      add(arguments[0]);
      counter++;

      if (counter < 2) {
        return addTogether;
      } else {
        //console.log(sum);
      }
    } else {
      return undefined;
    }
  }
  return sum;
}
var sum = 0;
var counter = 0;

addTogether(2, 3);```
**Your browser information:**

Your Browser User Agent is: ```Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36```.

**Link to the challenge:**
https://www.freecodecamp.org/challenges/arguments-optional

#2

First of all. I feel like this code is “over engineered”, you shouldn’t need a function “add”, which just adds two values. Nor should you need a counter, or sum variable.

Second, the challenge calls the addTogether function at the end, and I believe the tests call this function to see if you passed or not. But this function is missing, and most of your logic is outside of all the functions. I would wrap your primary logic back into the addTogether function. :smiley: