Skip to content
Advertisement

Arrow function that expects 2 numbers and returns undefined if they aren’t numbers

Need help. Need an arrow function that expects 2 numbers as input (e.g. 1, 2) and returns the sum of the two numbers. If anything other than 2 numbers is passed, return undefined. Not sure where I’m going wrong on this.

const sum = (num1, num2) => {
    
if((num1.value !== 0)||(num2.value !== 0)){
    return undefined
}
    return num1 + num2
}
console.log(sum(4,4))

Just keeps returning undefined, and doesn’t go to finding sum.

Advertisement

Answer

Use isNaN

const sum = (num1, num2) => {
  if (isNaN(num1) || isNaN(num2)) {
    return undefined;
  }
  return num1 + num2;
};
console.log(sum(4, 4));
Advertisement