Arguments Optional-stuck on two tests

Tell us what’s happening:
Can solve 3 out 5 test cases. What am I overlooking?

Your code so far


[spoiler]function addTogether() {
  var checkNum = function(num) { 
    if (isNaN (num)) { 
         return undefined; } 
    else return num; };

  var x = arguments.length > 1;
  switch(x) {
     case true:  // if (arguments.length > 1)
            var a = checkNum(arguments[0]);
            var b = checkNum(arguments[1]);
          if (a === undefined || b === undefined) {
             return undefined;
          } else {
            return a + b;
          }
    break;

    case false:  // if (arguments.length == 1)
          var c = checkNum(arguments[0]);
         if (c === undefined) { 
              return undefined;
          } else {
          return function(y) {
            if (c === undefined || checkNum(y) === undefined) {
              return undefined;
            } else {
            return c + y;
          }
        } 
      }
    break;
  
  }
}[/spoiler]

addTogether(2,3);

Your browser information:

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

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

Use typeof num !== 'number' rather than isNaN(num). isNaN tests if a value can be coerced to a number: '3' and [2] can both be coerced to numbers (3 and 2 respectively), so your test does not work in those cases.

Thanks!!

I spent hours trying to figure out where the error was and totally overlooked that aspect of isNaN.

1 Like