Intermediate Algorithm Scripting - Arguments Optional

Tell us what’s happening:
Describe your issue in detail here.

Why it returns [Function]

addTogether(5, undefined) should return undefined.
// tests completed
// console output
5
5
[Function]
Your code so far

function addTogether(a, b=0) {

  if (typeof a !== "number" || typeof b !== "number") {

    return undefined 

  } else if (b === "undefined") {
    return undefined
  }

  if (a && b) {

    if (typeof a === "number" && typeof b === "number") {

      return a + b

    }

  } else if (!b) {

    return function(c) {
      if (typeof c !== "number") {
        return undefined 
      }
      return a + c

    }

  } 

}

let addNums = addTogether(2)(3)
console.log(addNums);

addNums = addTogether(2, 3)
console.log(addNums)

addNums = addTogether(2, undefined) 
console.log(addNums)

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 11; Nokia 3.4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Mobile Safari/537.36

Challenge: Intermediate Algorithm Scripting - Arguments Optional

Link to the challenge:

With b parameter having default 0, the zero will be used when there’s no second argument passed to the function, or when second argument is literally undefined.

You can add console.log(a, b); at the start of function to check that.

1 Like

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