Argument optional, what could be done to modify the below

I was stuck on this, but understood what’s going on only after console-logging+heavy commenting+python-tutoring+reverse-engineering solution from guides. My PC freezed however, and bunch of comments was lost, that’s a bummer :upside_down_face:

For now I am trying to understand how would I modify the below code to return undefined instead of [Function]. More info in the comments.

Btw there no such tests cases in the challenge like addTogether(4), for example.

function addTogether() {

  const [first, second] = arguments;
  console.log(first, second)

  if (!(Number.isInteger(first))) {
    return undefined;
  }

  if (second === undefined) {
    return (second) => addTogether(first, second);
  }

  if (!(Number.isInteger(second))) {
    return undefined;
  }

  return first + second;
}

//addTogether(2, 3);
//addTogether(5)(7);
console.log('case only 1 argument and its a number, output:', addTogether(4))
//case only 1 arg and its a number, output: [Function]

Why are you trying to do this? It’s very different than the instructions.

I would replace the spot where you return a function with a return undefined and then every time you return a function you would instead return undefined.

1 Like

Yeah, I know I am not following instructions right now.
I just was a bit stuck with this case of input:

addTogether("https://www.youtube.com/watch?v=dQw4w9WgXcQ") should return undefined

where input is 1 argument and not a number

So I did some tests as above(only 1 argument which is number) and became curious about the question I asked.

It seemed little strange:
Why my function handles cases when only 1 argument is not a number, but doesn’t handle cases with only 1 argument which is a number

I’m not sure what you mean by ‘hold cases’? You handle both relevant cases with one argument here:

1 Like

I guess I should use word ‘handle’ here.

this works for input like:

addTogether('somestring')

or

addTogether([11])

or whatever input when we have only 1 argument and it’s not number.

However it doesn’t work for cases like addTogehter(9) or addTogether(999) or something like that.

So when I saw 2 cases like below:

console.log(addTogether('whatever but not a number'))//undefined
console.log(addTogether(23));//[Function]

It seemed like something could be done.

A function is returned so this case works out right

Waiting: addTogether(5)(7) should return 12.

1 Like

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