Intermediate Algorithm Scripting - Arguments Optional

hello,
could someone tell me y my code can not pass the last condition?
function addTogether() {
let [arg1, arg2] = arguments;

function isNum(num) {
return Number.isInteger(num);
}

if (!isNum(arg1)) {
return undefined;
}
else if (isNum(arg1) && isNum(arg2)) {
return arg1 + arg2;
}
else if (!arg2) {
return function(newArg) {
if (isNum(newArg)) {
return arg1 + newArg;
}
}
}
}

Your code so far

function addTogether() {
  let [arg1, arg2] = arguments;

  function isNum(num) {
    return Number.isInteger(num);
  }

  if (!isNum(arg1)) {
    return undefined;
  }
  else if (isNum(arg1) && isNum(arg2)) {
    return arg1 + arg2;
  }
  else if (!arg2) {
    return function(newArg) {
      if (isNum(newArg)) {
        return arg1 + newArg;
      }
    }
  }
}
 


console.log(addTogether(5, undefined))
console.log(addTogether(2, "3"))
console.log(addTogether(5)(7))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Arguments Optional

Link to the challenge:

You are not correctly handling the case where the second argument passed to the function is not a number. In that case, undefined should be returned. Instead, you have:

  else if (!arg2) {
    return function(newArg) {
      if (isNum(newArg)) {
        return arg1 + newArg;
      }
    }
  }

so you end up returning a function instead of undefined.

1 Like

Thanks. I see, but when I write:
if (!isNum(arg1) | !isNum(arg2) {
return undefined;}
another error occurs results in not passing :
addTogether(5)(7)` should return 12
Could u help abit?

I think you meant to put a || between those conditions. Either way, you can not write that because one of the possibilities is that only one argument can passed to the function and if it is a number, you are supposed to return a function. As you probably already know, if a second argument is not passed to a function, !isNum(arg2) would evaluate to true.

You are going to need to check the number of arguments passed to the function as part of your logic. If two arguments are passed to the function, then what you wrote above (using || instead of |) would work. But if only one argument is passed (and is a number), then you must return the function.

On a side note, there is nothing in the instructions to indicate that only integers would be passed as numbers. You could have a decimal number passed to the function, but your isNum function would not return the correct Boolean value. There is not test for this case, but it would make your code more robust.

1 Like

Oh, I understand now, but how I am supposed to cover both? to ask the function to return undefined if arg2 is a falsey value but goes through second function if there is no arg2 at all?

You can change this one if statement condition to handle both of the following cases:

  1. arg1 is not a number
  2. the number of arguments passed to the function is more than 1 AND arg2 is not a number.

If you do that, you will pass this challenge.

1 Like

You should not be checking if arg2 is falsy. What if the second argument was 0? That is a valid number.

Thanks. I finally made it work.