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:

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?

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?

Thanks. I finally made it work.

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