Build an Optional Arguments Sum Function - Build an Optional Arguments Sum Function

Tell us what’s happening:

I having trouble with #5 and #8.

#5 - my code seems to think that null and undefined are the same. I’m not sure how to differentiate between the two.

#8 if never seen a function be called like this addTogether(5)(7) - i thought a function had to one set of brackets (e.g addTogether(5, 7) ) - so because i’ve never seen that before i don’t know how to treat it

Your code so far

function addTogether(arg1, arg2 = null) {
  if (typeof arg1 !== 'number') {
    return undefined
  } else if (typeof arg2 !== 'number') {
    if (arg2 !== null) {
      return undefined
    } else {
      return function() {
      } 
  }
  }
  else {
   return arg1 + arg2
   }

}



console.log(addTogether(2, null));
console.log(addTogether(5, undefined));
console.log(addTogether(5)(7));



Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0

Challenge Information:

Build an Optional Arguments Sum Function - Build an Optional Arguments Sum Function

https://www.freecodecamp.org/learn/full-stack-developer/lab-optional-arguments-sum-function/build-an-optional-arguments-sum-function

you can do this because addTogether(5) should return a function, and you can call a function adding (7) next to it

can you give more details to this please?

image

be careful of what comparison you use

wait, I think you have met an issue with default values

if you do this, the default value of the second parameter is given to it

are maybe the rest parameter or the arguments object taught before this lab? it looks like they are needed, and if they are not we may have put the lab in the wrong place

I have created an issue for this

ok, i’ve fixed #8. Just #5 to go

else if (typeof arg2 !== 'number') {
    if (arg2 !== null) {
      return undefined

are you saying i should try != instead? that still doesn’t work

or is the lab in the wrong place like you said and it’s not what i’m doing that is the problem?

Please share your full updated code.

the issue is that you should return a function with addTogether(5) but return undefined with addTogether(5, undefined)

in both of these cases arg2 is null, your approach can’t distinguish them

updated code:

function addTogether(arg1, arg2 = null) {
  if (typeof arg1 !== 'number') {
    return undefined
  } else if (typeof arg2 !== 'number') {
    if (arg2 !== null) {
      return undefined
    } else {
      return function(number) {
        if (typeof number !== 'number') {
          return undefined
          } else {return arg1 + number}
      } 
  }
  }
  else {
   return arg1 + arg2
   }

}

so what approach do i need to take?

Use console.log to determine what happens to arg2 in each case.

function addTogether(arg1, arg2 = null) {
  console.log(arg2)
console.log(addTogether(5, undefined))
console.log(addTogether(5))

You’ll need to find a way to differentiate these inputs. Maybe you can’t use arg2 = null ?

i changed it to rest parameters and it works

thanks for your help

1 Like

is that something you have learned in the curriculum? or is that knowledge coming from somewhere else?

1 Like

i did Google it - so that is where i learned how to use it for this project.

I thought it had been mentioned somewhere earlier in the curriculum - not sure how in-depth it was though

it was mentioned for destructuring arrays, here: https://www.freecodecamp.org/learn/full-stack-developer/lecture-working-with-arrays/what-is-array-destructuring-and-how-does-it-work

not for function parameters

yeah, i didn’t even consider i could use rest parameters there, i had to look it up and found a different way to check the number of arguments

1 Like