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

318
Visualizações
Function does not work when entering number javascript

I have a function that reverses a string. I would like the function to return "Null" when a number is entered however when i input a number my function stops working. Is there something i am missing?

function reverseWords(words) {
    let eachLetter = words.split("");

    let reverseLetter = eachLetter.reverse();

    let combineLetter = reverseLetter.join("");

    if (typeof words !== "string") {
        return null;
    } else {
        return combineLetter;
    }
}

console.log(reverseWords(89));

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

the operations you try to perform before the if are not possible with numbers.

Put validation as the first thing your function does and everything will work.

about 4 years ago · Juan Pablo Isaza Relatório

0

function reverseWords(words) {

if (typeof words !== "string") {
    return null;
} else {
        
   //Split the string 
  let eachLetter = words.split("");

  let reverseLetter = eachLetter.reverse();

  let combineLetter = reverseLetter.join("");


    return combineLetter;
}

}

console.log(reverseWords('89'));

You cannot apply 'split' method on numbers. First, check for the numeric value and perform the split function only when it is a string.

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