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

124
Vistas
Generate Password through Javascript and render in back to HTML

So I am trying to build a simple password generator via JavaScript and the function just won't return anything, it will return nothing(blank).

let chars = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let randomPass = ""
let passLen = 16


function ranNum() {
    for ( let i=0; i<passLen.length; i++){
        let randomLetter = Math.floor(Math.random() * chars.length) + 1
        randomPass += chars.charAt(randomLetter)
        
    }
    return randomPass
}

function genPass() {
    document.getElementById("div1").textContent = ranNum()
}
<button onclick="genPass()">Generate</button>
<div id="div1"></div>

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

0

Your for loop is not executing because you are asking for passLen.length, and passLen is an integer it wont have a length, and then the loop won't run. just change your line for ( let i=0; i<passLen.length; i++){ to for ( let i=0; i<passLen; i++){ and it will run fine.

let chars = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let randomPass = ""
let passLen = 16


function ranNum() {
console.log('generate');
    for ( let i=0; i<passLen; i++){
        let randomLetter = Math.floor(Math.random() * chars.length) + 1
        randomPass += chars.charAt(randomLetter);
        console.log(randomLetter);
        
    }
    return randomPass
}

function genPass() {
    document.getElementById("div1").textContent = ranNum()
}
<div id="div1">test</div>

<button onclick="genPass()">Generate password</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

It's because you have the length property on passLen and the variable value is an int.

 for ( let i=0; i<passLen; i++){

    let randomLetter = Math.floor(Math.random() * chars.length) + 1
    randomPass += chars.charAt(randomLetter)
 }
about 4 years ago · Juan Pablo Isaza Denunciar

0

In your for loop, passLen is not an array, it's already a length. So you don't need passLen.length only passLen.

let chars = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let randomPass = ""
let passLen = 16


function ranNum() {
  for (let i = 0; i < passLen; i++) {
    let randomLetter = Math.floor(Math.random() * chars.length) + 1
    randomPass += chars.charAt(randomLetter)
    console.log(randomPass)
  }
  return randomPass
}

function genPass() {
  document.getElementById("div1").textContent = ranNum()
}
<button onclick="genPass()">Generate</button>
<div id="div1"></div>

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