Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

114
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda