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

265
Views
¿Cómo condiciona la entrada el número de caracteres de la contraseña? JavaScript

Estoy haciendo ejercicios en javascript y me encontré con un problema. el usuario debe elegir la longitud de los caracteres en la contraseña; sin embargo, si selecciono 4 caracteres en la entrada, obtengo 3. ¿Por qué?

mi pensamiento fue que sirviera para elegir la cantidad de caracteres y generar una contraseña tantos caracteres como haya. donde me equivoco pido ayuda!

 function generirajLozinku(pLength) { var keyListAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", keyListInt = "123456789", keyListSpec = "", password = '@'; var len = Math.ceil(pLength / 2); // mijenanjem ovog broja mijenja se duzina lozinke len = len - 1; var lenSpec = pLength - 2 * len; for (i = 0; i < len; i++) { password += keyListAlpha.charAt(Math.floor(Math.random() * keyListAlpha.length)); password += keyListInt.charAt(Math.floor(Math.random() * keyListInt.length)); } for (i = 0; i < lenSpec; i++) password += keyListSpec.charAt(Math.floor(Math.random() * keyListSpec.length)); password = password.split('').sort(function() { return 0.5 - Math.random() }).join(''); return password; } function myFunction() { var x = document.getElementById("num").value; document.getElementById("demo2").innerText = generirajLozinku(x); }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Document</title> </head> <body> <section> <div> <h1>Your strong password marker</h1> <p>Password length</p> <input type="number" name="izaberi boj karaktera!" id="num" min="4" max="10"> <br> <button onclick="myFunction()">Generate</button> <p id="demo2">Your password</p> </div> <div> </div> </section> </body> </html>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

La razón por la que su código no funciona es que este es el último bucle en el que intenta completar los caracteres que faltan:

 for (i = 0; i < lenSpec; i++) password += keyListSpec.charAt(Math.floor(Math.random() * keyListSpec.length));

Elige un carácter aleatorio de keyListSpec , pero definió keyListSpec como keyListSpec = "" , por lo que no hay nada para elegir.

La elección de diseño de su código es realmente extraña. Las contraseñas generadas tienen un patrón muy predecible, lo que reducirá drásticamente las posibles combinaciones. El sufrimiento de los personajes de la contraseña no ayuda allí. Además de eso sort(function() { return 0.5 - Math.random() }) no es una forma muy buena de barajar.

Además Math.random() no es una buena opción cuando se trata de generar contraseñas. Si está buscando un generador de contraseñas escrito en JavaScript, debe consultar, por ejemplo , Genere una cadena de contraseña aleatoria con requisitos en javascript en particular en esta respuesta .

about 4 years ago · Juan Pablo Isaza Report

0

Parece que no funcionaba para los pares, es decir, 4, 6, etc. La forma en que manejó las probabilidades frente a los pares se veía mal. Hice una versión más simple para ti.

 function generirajLozinku(pLength) { var keyListAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789", password = '@'; var len = pLength - 1; for (i = 0; i < len; i++) { password += keyListAlpha.charAt(Math.floor(Math.random() * keyListAlpha.length)); } password = password.split('').sort(function() { return 0.5 - Math.random() }).join(''); return password; } function myFunction() { var x = document.getElementById("num").value; document.getElementById("demo2").innerText = generirajLozinku(x); }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Document</title> </head> <body> <section> <div> <h1>Your strong password marker</h1> <p>Password length</p> <input type="number" name="izaberi boj karaktera!" id="num" min="4" max="10"> <br> <button onclick="myFunction()">Generate</button> <p id="demo2">Your password</p> </div> <div> </div> </section> </body> </html>

about 4 years ago · Juan Pablo Isaza Report

0

Revisa tu código y coméntalo. pLength = 4 porque keyListSpec = "" entonces el siguiente código

 // keyListSpec is "", so keyListSpec.charAt always return "" for (let i = 0; i < lenSpec; i++) password += keyListSpec.charAt(Math.floor(Math.random() * keyListSpec.length)); function generirajLozinku(pLength) { var keyListAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", keyListInt = "123456789", keyListSpec = "", password = '@'; var len = Math.ceil(pLength / 2); // mijenanjem ovog broja mijenja se duzina lozinke len = len - 1; var lenSpec = pLength - 2 * len; // if pLength = 4 , len is 1 and lenSpec is 2 // here password is "@" for (let i = 0; i < len; i++) { password += keyListAlpha.charAt(Math.floor(Math.random() * keyListAlpha.length)); password += keyListInt.charAt(Math.floor(Math.random() * keyListInt.length)); } // if pLength = 4 , len is 1 and lenSpec is 2 // here password length is 3 "@xx" console.log("keyListSpec", keyListSpec); // keyListSpec is "", so keyListSpec.charAt always return "" for (let i = 0; i < lenSpec; i++) password += keyListSpec.charAt(Math.floor(Math.random() * keyListSpec.length)); password = password.split('').sort(function() { return 0.5 - Math.random() }).join(''); return password; }
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!