Intermediate Algorithm Scripting - Arguments Optional

Tell us what’s happening:
Describe your issue in detail here.
these three doesn t pass and I don t quite understand how to deal with them because it says when I try to call tgem the way they the test are written they return TypeError: addTogether(…) is not a function and if I fix them so I change the function call to something more sensible the code still doesn t pass
and I don t quite understand what does it mean to return a function like should I make another function and return it?

  • addTogether(5) should return a function.

  • Failed:addTogether(5)(7) should return 12.

  • Failed:addTogether(2)([3]) should return undefined.
    Your code so far

function addTogether(numA, numB) {
  let g;
  if (typeof numA === "number" && typeof numB === "number") {
     g = numA + numB;
  } else {
     g = undefined;
  }
  if (typeof numB === "object" && Array.isArray(numB)) {
    g = undefined;
  }
  if (numB == null || numA == null) {
    
  }
  return g;
}

addTogether(5);

Your browser information:

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

Challenge: Intermediate Algorithm Scripting - Arguments Optional

Link to the challenge:

You need another case where there is only 1 argument.

If there is only one argument, then you can return a new arrow function that takes 1 argument and passes it to back to the existing add function along with the previous argument, it’s recursive.

That sounds very complicated to explain but you do return a function in the previous problem.

I’d also watch you - you’re coding to the tests here instead of the instructions. You should behave the same for any non-number passed as the second argument.

This will give you confusing results between 1) no value passed for numB and 2) undefined passed for numB

ok so I have to use an arrow function, I have found a solution on youtube and I think I understand how it works it checks on the first line whether the inputs are integers or not if they are it sums them and return the sum if they aren t it uses an arrow function in order to return a function with the empty arguments passed but the code still doesn t pass because it doesn t pass this requirement I have tried hard coding it but it doesn t make sense but if I do that it doesn t return the second condition in case where it supposed to be returning the function so I have no Idea how can make the code pass. Failed:addTogether(5, undefined) should return undefined. this one doesn t pass.

  const [arg1, arg2] = arguments;
  const isNum = (num) => Number.isInteger(num);

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

console.log(addTogether(5, undefined)); the solution I found that doesn t work

You don’t have to use an arrow function.

The problem statement doesn’t say anything about integers

Do you see a way to modify this code so that if either argument is undefined it just returns undefined?

  const [arg1, arg2] = arguments;
  const isNum = (num) => Number.isInteger(num);

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

console.log(addTogether(5, undefined));

i mean like this

Why are you checking for an integer?

You don’t need this case if your other cases are correct

This checks if arg2 is falsy, not if it was not given

yeah but it gives same results if I change it the problem is that it returns a function instead of undifined

  const [arg1, arg2] = arguments;
  const isNum = (num) => Number.isInteger(num);

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

console.log(addTogether(5, undefined));

Stop checking for an integer! There are other types of numbers.

This isn’t the same as checking if the second argument was never passed. You need to check the number of arguments passed. You can do that by checking the length of the arguments object.

ah okay so I had to check for the length and if it doesn t meet anything it returns undefinded ok thank you

You also need to check for a number and not an Integer.

The test cases are actually missing one, addTogether(3.4, 6)

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