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

219
Vistas
how to check if a password contains an integer and return an object with a boolean representation of that value

Trying to create a function that checks a password for integers and then return an object which will store a boolean representing whether the password contains an integer, and then destructure the boolean out of the function, but I keep getting this problem...

function checkForInteger(password) {
  const arrayOfIntegers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
  let intBoolean = {};
  arrayOfIntegers.forEach((int) => {
    if (password.includes(int)) {
      intBoolean = { integerIsPresent: password.includes(int) };
    } else {
      intBoolean = { integerIsPresent: password.includes(int) };
    }
  });
  return intBoolean;
}
checkForInteger("3gufr"); //returns {integerIsPresent:false}

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

0

You're replacing intBoolean each time through the loop. So if 3 is found, you'll set it to {integerIsPresent: true} on that iteration, but then replace it with {integerIsPresent:false} on the remaining iterations.

Initialize it to false before the loop, and only set it to true when you get a match.

function checkForInteger(password) {
  const arrayOfIntegers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
  let intBoolean = {
    integerIsPresent: false
  };
  arrayOfIntegers.forEach((int) => {
    if (password.includes(int)) {
      intBoolean.integerIsPresent = true;
    }
  });
  return intBoolean;
}
console.log(checkForInteger("3gufr"));

You can also simplify it by using Array.some()

function checkForInteger(password) {
  const arrayOfIntegers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
  let intBoolean = {
    integerIsPresent: arrayOfIntegers.some(int => password.includes(int))
  };
  return intBoolean;
}
console.log(checkForInteger("3gufr"));

or you can use a regular expression.

function checkForInteger(password) {
  return {
    integerIsPresent: !!password.match(/\d/)
  };
}
console.log(checkForInteger("3gufr"));
password.match() returns an array when there's a match. !! converts that to a boolean.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could probably adjust your logic a little. forEach doesn't allow to you break out of the loop - for/of does.

Loop over the array. If there is a match return the object containing the match, otherwise return an object with a false value.

const arrayOfIntegers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

function checkForInteger(password) {  
  for (const el of arrayOfIntegers) {
    if (password.includes(el)) {
      return { integerIsPresent: true };
    }
  }
  return { integerIsPresent: false };
}

console.log(checkForInteger('3gufr'));
console.log(checkForInteger('nnh5dd'));
console.log(checkForInteger('abcdef'));

about 4 years ago · Juan Pablo Isaza Denunciar

0

this is how I resolved the issue though, thanks a lot @Barmar

function checkForInteger(password) {
  const arrayOfIntegers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  const checkPassword = (el) => password.includes(el);
  const integerIsPresent = arrayOfIntegers.some(checkPassword);
  // console.log(integerIsPresent);
  return integerIsPresent;
}

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