Arguments Optional - return addsecond - 2 cases

Hi there!
Help to understand, plz, why we need to return - return addsecond - if we not return it cause error ‘addTogether(…) is not a function’…?
why ‘return first + second’ not enough?
And - what is the place to where 'return addsecond ’ - ?

this code is part of solution2 freeCodeCamp Challenge Guide: Arguments Optional
for 2 numbers as arguments

function addTogether() {
  const [first, second] = arguments;
  
  if (arguments.length === 1) {    // CURRYING
    function addsecond(second) {
        return first + second; 
    }
       return addsecond;  //  *** Note: returning a *function*
  }
return first + second;
 }

and where is the returning the function in this case?

function addTogether() {
 
  var [fst,scn] = arguments;
  
  var isNum = function(arg) { return Number.isFinite(arg); };

  if (arguments.length === 1 && isNum(fst)) {
    return function(dop) { //CURRY scn
      if (isNum(dop)) {
        return fst + dop;
      }
    }; // *** WHERE is returning unnamed function??
  }
  else if (arguments.length === 2 && isNum(fst) && isNum(scn)) {
    return fst + scn;
  }
}

If you now where it explained in details, plz, provide the link

You mean this line:

Well that’s how we are using this function in this code. Without this line it would be just code with addsecond function declared, and not used.

Consider to run this code(I mean whole solution 2) in pythontutor, for me it was super helpful.

A lot of thanks for the pitontutor! Great tool!

YES! I see now - this line ‘return addsecond’ is calling the addsecond! I thought until that it works line by line and returning someth to idontknowwhere ))

yep, pythontutor is great with recursion, currying and stuff like that

saved my brains from collapsing couple of times

1 Like

Two side notes here:

  1. var is a legacy feature. Don’t use var

  2. It doesn’t cost extra to use variable names that are understandable by humans :slight_smile:

oh! sorry, I use egyptian like shortens with no vowels

That was trendy back in the 70s, 80s, and 90s because it was a holdover from when Fortran had fixed line lengths.

Nowadays, there are not line length limits and the final deployed JS code will get minified, so readability is preferred.

1 Like

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