Intermediate Algorithm Scripting - Arguments Optional

Tell us what’s happening:
can some one give me a clue for this task
especially in accessing this:
addTogether(5)(7)

Your code so far

function addTogether(...arr) {
  let x = arr[0];
  let y = arr[1];
  if(typeof(x) !== "number" || typeof(y) !== "number" ){
    return undefined
  }
  if(y === undefined){
    return (y)
  }
  return arr[1] + arr[0]
}

console.log(addTogether(2,3));

Your browser information:

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

Challenge: Intermediate Algorithm Scripting - Arguments Optional

Link to the challenge:

Tell us what’s happening:
here is the only problem I have left addTogether(5, undefined)
should return undefined

Your code so far

function addTogether() {
  const [firstNum, secondNum] = arguments;
  const numberOrNot = (num) => typeof(num) === "number";

  if(!numberOrNot(firstNum)){
    return undefined
  }
  else if(numberOrNot(firstNum) && numberOrNot(secondNum)){
    return (firstNum * 10 + secondNum * 10)/ 10
  }
  else if(!secondNum){
    return (thirdNum) =>{
      if(numberOrNot(thirdNum)){
        return thirdNum + firstNum
      }
    }
  }
  return 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/116.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Arguments Optional

Link to the challenge:

my code is returning [function ]
when it should be returning undefined

Hi,

Sometimes it is better to write a longer version of the code that you are certain is doing what you think it is doing. And then after the code is working, you can minimize it and remove excess line if you want to.

For this task, undefined should be returned if the argument(s) is not a number.

if(typeof(x) !== "number" || typeof(y) !== "number" ){
    return undefined
  }

Looking at the code above, what happens if only one argument is passed? Keep in mind that the || operator will continue testing until a truthy value is found, or all expressions are false.

Secondly, in order to know whether it should return a function or a sum, the function should know how many arguments there are, and make a decision using that information.

if(y === undefined){
    return (y)
  }

Lastly, this does not return a function.

It may be worth it to take a look into closures:

By the standard of this challenge, secondNum will be undefined if:

  1. A second argument was not passed, or
  2. The second argument is undefined (addTogether(5, undefined)

To determine which of these it is, or which action to take, it will be a good idea to check how many arguments addTogether() is called with.

@ereal3755 Please do not create duplicate topics for the same challenge/project question(s). I have merged your threads.

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