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

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

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 Denunciar

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 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