Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda