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

317
Vistas
JavaScript: eliminate number with decimal .0

I need your advice (I'm a beginner in JS).This is my code:

function fn(pin) {

  if (pin.length === 4 && !isNaN(pin) && pin % parseInt(pin) === 0 && Number(pin) > 0) {
    return true;
  }
  return false
}

console.log(fn("123a.78")) // false

So i check the length, that it is a number, that it is integer and that it is bigger than 0. But how should i write the condition to eliminate strings of this type: "12.0" (because it also has length of 4 and is also integer)? If i try conditions with pin.parseFloat or parseInt, it will affect strings like this type "1234" as well. Or maybe i write them in the wrong way...

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I would go with a regular expression to validate the input. The regular expression to validate 4 digits only would look like this: /^[0-9]{4}$/

Regular expressions have methods that allow them to test if variables are of their type. For instance:

const pin = '1234';
const pin2 = '12.4'
const regExp = /^[0-9]{4}$/

regExp.test(pin) //returns true

whereas:

regExp.test(pin2) //returns false

You can use this validation as the condition in your if/else conditions to execute the code you need.

freecode tutorial on Regular Expressions https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/#es6

Regular expressions playground: https://regex101.com/

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