Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
cómo verificar si una contraseña contiene un número entero y devolver un objeto con una representación booleana de ese valor

Intentando crear una función que verifique una contraseña para números enteros y luego devuelva un objeto que almacenará un valor booleano que representa si la contraseña contiene un número entero, y luego desestructurar el valor booleano de la función, pero sigo teniendo este problema...

 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 answers
Answer question

0

Está reemplazando intBoolean cada vez que pasa por el bucle. Entonces, si se encuentra 3 , lo establecerá en {integerIsPresent: true} en esa iteración, pero luego lo reemplazará con {integerIsPresent:false} en las iteraciones restantes.

Inicialízalo en false antes del ciclo y solo configúralo en true cuando obtengas una coincidencia.

 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"));

También puede simplificarlo usando 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"));

o puede usar una expresión regular.

 function checkForInteger(password) { return { integerIsPresent: !!password.match(/\d/) }; } console.log(checkForInteger("3gufr"));
password.match() devuelve una matriz cuando hay una coincidencia. !! lo convierte en un booleano.

about 4 years ago · Juan Pablo Isaza Report

0

Probablemente podrías ajustar un poco tu lógica. forEach no le permite salir del bucle - for/of sí.

Bucle sobre la matriz. Si hay una coincidencia, devuelve el objeto que contiene la coincidencia; de lo contrario, devuelve un objeto con un valor falso.

 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 Report

0

así es como resolví el problema, muchas gracias @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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!