Arguments Optional: How "second" could be a number since `(second === undefined)`?

Hello,
Arguments Optional
In the FCC solution 1:

function addTogether() {
  const [first, second] = arguments;
  // First argument is not a number
  if (typeof(first) !== "number") {
    return undefined;
  }
  // First argument is a number
  //  and second argument is not defined
  else if (second === undefined) {
    function addSecond(second) {
      // New argument is not a number
      if (typeof(second) !== "number") {
        return undefined;
      }
      // New argument is a number
      else {
        return first + second;
      }
    }
    // Note: returning a *function*
    return addSecond;
  }
  // First argument is a number
  //  and second argument is not a number
  else if (typeof(second) !== "number") {
    return undefined;
  }
  // First argument is a number
  //  and second argument is a number
  else {
    return first + second;
  }
}
 console.log(addTogether("https://www.youtube.com/watch?v=dQw4w9WgXcQ"))


In else if (second === undefined) { section,
I don’t understand this part:

 // New argument is a number
      else {
        return first + second;
      }

How “second” could be a number since (second === undefined) ?

You need to be careful about the scope. second is redefined, and inside the scope of the redefinition the new version of the variable shadows the old.

is it !== "number" include undefined ?
I think no, but I would like have confirmation.

This is the redefinition of second for the scope of the function addSecond. This redefinition shadows the outer definition here:

Yes, I understand, it’s clever.

Even though it is more terse, I find the other solution that is posted to be much clearer.

it’s a recursive solution. isn’t it ?

Kinda? I wouldn’t really call it ‘recursive’.

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