0

So, basically, I am trying to generate a password when I click a button. The program generating the password once but when I click it again due to the fact that a new Randomized number isn't generated the program doesn't give a new password. Here is the CODE:

    let password  = ["NyiNE7!38vs","SyiMA4*78dt","SuoSO6]40db","LeaDU9'11ln","QooGU9!09nk","SeuXI1_63sp","SieKY6)07zk","GaaDI9'30gn","BoyLY4|74ct","BuaZI0+69vl"]
  function Random() {
let i = Math.floor(Math.random() * 9)
}
 i = Math.floor(Math.random() * 9)
const div = document.getElementById('password')
const button = document.getElementById('generate')
button.addEventListener('click' , generatePassword , Random)
function generatePassword(){
div.innerHTML = password[i]
}
  • The generatePassword function only updates the div based on your password list and i. i is generated outside or the function. – pyb Jul 22 '21 at 05:34
  • Technically your code is not so much of a generator. It is rather selecting a random password. If over ten users were using it, they may share the same password. Instead, you can generate larger numbers and encode them in ASCII (65 gives A, 66 is B, etc.). You could then repeat the algorithm for as many characters as you'd like in the password. – pyb Jul 22 '21 at 05:38

2 Answers2

1

Quite a bit going on here, but has some basic errors.

I have fixed those errors so that it creates a password when first run, and when you click the generate button thereafter.

let password  = ["NyiNE7!38vs","SyiMA4*78dt","SuoSO6]40db","LeaDU9'11ln","QooGU9!09nk","SeuXI1_63sp","SieKY6)07zk","GaaDI9'30gn","BoyLY4|74ct","BuaZI0+69vl"]
// returns a random numbr;
function Random() {
   let i = Math.floor(Math.random() * 9)
   return i; // note i return the random number!
}
// returns a new password using Random()
function newPw(){
    return password[Random()];
}

const div = document.getElementById('password')
const button = document.getElementById('generate')
button.addEventListener('click' , generatePassword)
// if you want a pre-populated password;
generatePassword();

// Sets a new random password
function generatePassword(){
   div.innerHTML = newPw();
}

I have shortened this down to use a far more secure method of generating passwords, you can find plenty of examples of random password generators on SO.

const div = document.getElementById('password')
const button = document.getElementById('generate')
button.addEventListener('click' , generatePassword);
// if you want a pre-populated password;
generatePassword();
// taken from https://stackoverflow.com/questions/9719570/generate-random-password-string-with-requirements-in-javascript
function generatePassword(){
    var randPassword = Array(10).fill("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").map(function(x) { return x[Math.floor(Math.random() * x.length)] }).join('');var randPassword = Array(10).fill("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").map(function(x) { return x[Math.floor(Math.random() * x.length)] }).join('');
    div.innerHTML = randPassword;
}
MartinWebb
  • 1,998
  • 1
  • 13
  • 15
0

Your code has some problems, first the variable i is outside the generatePassword method, and hence is not getting updated. I have made some changes to your code to make it work, basically call generatePassword onclick and inside it call your random function. You only want to call the functions in an ordered manner when needed. Find code below:

  let password  = ["NyiNE7!38vs","SyiMA4*78dt","SuoSO6]40db","LeaDU9'11ln","QooGU9!09nk","SeuXI1_63sp","SieKY6)07zk","GaaDI9'30gn","BoyLY4|74ct","BuaZI0+69vl"]
  function getRandom() {
    return Math.floor(Math.random() * 9)
}
 const div = document.getElementById('password')
 const button = document.getElementById('generate')

function generatePassword(){
    const randomIndex = getRandom()
    div.innerHTML = password[i]
}


 button.addEventListener('click' , generatePassword)

Also, as pointed out in the comment, you dont want your password list to be of finite length, rather generate new passwords every time.

Ankit Gupta
  • 175
  • 6