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

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

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 Report

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 Report

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 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!