"Arguments Optional" algorithm question

Hello all, I’m working on the “Arguments Optional” algorithm and found what I thought to be a solution, but I got a problem at runtime that I can’t quite put my finger on.

Here is my code:

function addTogether() {
  function ckargs(arr) {
  for (let i = 0; i < arr.length; i++) {
    if (i == 3) { return 0; }
    if (typeof arr[i] != "number") { return 0; }
  }
  return true;
  }
  var args = [...arguments];
  var firstarg = args[0];

  if (!ckargs(args)) return;

  if (args.length == 2) {
    if (!ckargs(args)) return; 
// just to show that this, in theory, is doing the right calculation:
console.log(firstarg + args[1]);
// however, this will return undefined:
    return (firstarg + args[1]);
  }
  else if (args.length == 1) {
    return function (a, b) {
      addTogether(firstarg, b)
    };
  }

}

addTogether(3)(2);

This will print the correct response “5” to the console, but will return undefined. It fails requirement #2 of the challenge but passes all the others.

Can anyone explain to me why this function is returning undefined in the example, rather than the value that is successfully printing to the console?

In studying the sample answer #1, I have successfully modified my code to pass the algorithm test, by rewriting the function to not be recursive in the case of one argument.

What I’m trying to understand is: was my original error due to a scope issue with the recursive function call not allowing the function to return properly? Is there a way to rewrite my original code to return the correct value when the function is called recursively?

Any pointers would be appreciated.

A few things.

The instructions say:

“If only one argument is provided, then return a function that expects
–> one <–
argument and
–>returns<--
the sum.”

I’ve tried to give you a few subtle hints as to where the problems are :slight_smile: