Build a Password Generator App - Build a Password Generator App

Tell us what’s happening:

help me anyone my code is not working. i have tried everything

Your code so far

let password = '';

const characters = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','!','@','#','$','%','^','&','*','(',')'];
const arrayLength = characters.length - 1;
const generatePassword = passwordLength => {
  for (let i = 0; i < passwordLength; i++) {
    let math = Math.round(Math.random() * (arrayLength - 0) + 0);
    password += characters[math]; 
  }
  return password
}
generatePassword(7);
console.log(`Generated password:${password}`);
console.log(password.length);
console.log(generatePassword(4));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36

Challenge Information:

Build a Password Generator App - Build a Password Generator App

Your code contains global variables that are changed each time the function is run. This means that after each function call completes, subsequent function calls start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

@khushi3

The way you initialize your password is global, which means the password value keeps appending the new password to the previous one each time you call the function. Therefore, you need to move it inside the generatePassword() function.

I recommend changing const characters to const CHARACTERS since the value never changes, following the convention of using uppercase for constant variables.

1 Like

thank you it worked…

1 Like