How to add all the arguments, wether there are two or more

Tell us what’s happening:
Hello,
I would like to add arguments together, if there is two arguments then add a+b, but if there is more than 2 arguments, the result here is undefined I don’t understand why ?
Could anyone please help. Thank you.
Your code so far


function addTogether() {
// Function to check if a number is actually a number
function checkNum(num) {
  return (typeof num === "number");
};

if (arguments.length > 1) {
  // Check if we have two arguments and if they are numbers
  // Return the sum if they are both numbers
  let args = [... arguments]
  if (checkNum(args)) {
    return args.reduce((a,b)=> a+b,0);
  } else {
    return undefined;
  }
} else if (arguments.length === 1) {
  // If only one argument was found, return a new function
  let first = arguments[0];
  if (checkNum(first)) {
    // Return function that expect a second argument.
    function addSecond(second) {
      // Check if the new argument is a number
      if (checkNum(second)) {
        return first + second;
      } else {
        return undefined;
      }
    };
    return addSecond;
  } else {
    return undefined;
  }
} 
}
console.log(addTogether(2, 3));

  **Your browser information:**

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

Challenge: Arguments Optional

Link to the challenge:

Hi there :wave:

Because checkNum takes a number, not an array. Anyway, since you’re already using the spread syntax you could simply do the following:

const addTogether = (...args) => (
  args
    .filter(item => typeof item === 'number')
    .reduce((accumulator, value) => accumulator + value, 0)
);

this will never work, args is an array, so checkNum says false

Thank You for your time and commitment.

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