Help! Passing the last test case, and pointing the error in code!

How do I pass the last test case which is addTogether(2)([3])? Also, why is the following code unable to pass it?

function addTogether() {
let args = Array.from(arguments);
let temp = args.map(item => Number.isInteger(item));
if (temp.includes(false)) {return undefined;}
if (args.length==2) {
  return args[0] + args[1];
}
else if (args.length==1) {
  return (temp) => temp + args[0];
}
}

console.log(addTogether(2)([3]));

Challenge: Arguments Optional

Link to the challenge:

function addTogether() {
  let args = Array.from(arguments);
  let temp = args.map(item => Number.isInteger(item));
  if (temp.includes(false)) {
    return undefined;
  }
  if (args.length == 2) {
    return args[0] + args[1];
  } else if (args.length == 1) {
    // This returned function needs to check if temp
    // is a valid function argument
    return (temp) => temp + args[0];
  }
}

console.log(addTogether(2)([3]));

For what its worth, it would help others read and debug your code if you use some clearer formatting and variable names.

1 Like

Thanks for the advice, @JeremyLT ! I’ll definitely take it into account!

After implementing what you’ve said, this seems to be an appropriate version of the code:

function addTogether() {

    let args = Array.from(arguments);
    let temp = args.map(item => Number.isInteger(item));
    if (temp.includes(false)) { return undefined; }
    if (args.length == 2) {
        return args[0] + args[1];
    }
    else if (args.length == 1) {
        // check if x is a valid function argument
        return function (x) {
            if (Number.isInteger(x)) {
                return x + args[0];
            }
            else {
                return undefined;
            }
        }
    }
}

console.log(addTogether(2)([3]));
1 Like

Nice work! It’s a bit of a tricky challenge.

1 Like

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