Optional Arguments Sum Function

I am stuck on the last portion of this tests. I thought that i was returning the function similar to the provided example but I think I may need help understanding the concept.

Here is my code so far:

//

function addTogether(…args) {

let arg1 = args[0];

let arg2 = args[1];

function addNew(next) {

return args\[0\] + next;

}

if (typeof(args[0]) === “number” && typeof(args[1]) === “number”) {

return arg1 + arg2;

} else if ((args[0]) !== “number” && typeof(args[1]) !== “number”) {

return undefined;

} else if (args.length === 1 && typeof(args[0]) === “number”) {

return addNew(next);

}

}

console.log(addTogether(5)(7))

//

I am receiving TypeError: addTogether(…) is not a function

Also I forgot the syntax to group the coding together on the site, so it looks jumbled.

Please format your code, right now it is not readable

When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

From what I can read, right now your code is not returning a value for addTogether(5, "3")

and it is not dealing with the case addTogether(5)("not a number")

Here is my full code:

function addTogether(...args) {
  let arg1 = args[0];
  let arg2 = args[1];
  function addNew(next) {
    return args[0] + next;
  }
  if (typeof(args[0]) === "number" && typeof(args[1]) === "number") {
    return arg1 + arg2;
  } else if ((args[0]) !== "number" && typeof(args[1]) !== "number") {
    return undefined;
  } else if (args.length === 1 && typeof(args[0]) === "number") {
    return addNew();
  } 
}

Yes. I am failing tests 7, 8, 9.

I would think about the order of your conditions.

Even if only argument is passed, both of these variables will have some sort of a value in them that can have its type checked.