Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
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.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Use isNaN

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

about 4 years ago · Juan Pablo Isaza Report

0

You are trying to access a submember of a number value, not an object, so you will always get something you don't want to get. This should work better :

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

You should also look what is isNaN which is a better way to check if you do receive a number or not :) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN

about 4 years ago · Juan Pablo Isaza Report

0

I think what are are looking for is something like this.

const sum = (num1, num2) => {
    if (typeof(num1) !== "number" || typeof(num2) !== "number")
        return "undefined";

    return num1 + num2;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!