I am writing a code to randomly generate a password for a user given a set of criteria. The program asks the user to select what character types to include. Once the user makes this selection, the code should generate the password for the user. My code goes through the series of prompts but does not generate the password. Could someone please tell me what I’m doing wrong?

//  Code
var generateBtn = document.querySelector("#generate");

// Declare variables for uppercase, lowercase, numeric, and special characters.
var upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var lowerCase = "abcdefghijklmnopqrstuvwxyz";
var numeric = "0123456789";
var special = "!@#$%^&*()_-+={}[];:'`~<,>.?/|";
var passwordLength;

// Function to check password length
function chooseLength() {
  passwordLength = prompt("How many characters would you like to include in your password?");

  // Use conditionals to restrict user to select password length between 8 and 128 characters inclusive

    if (passwordLength<8){
    alert("Password length must be at least 8 characters long");
    chooseLength();
    }else if (passwordLength>128){
    alert("Password length must be less than 129 characters");
    chooseLength();
    }else if (isNaN(passwordLength)){
    alert("Password length must be a number between 8-128 characters");
    chooseLength();
    }else{
    alert("Please select what character types you would like to be included in your password.");
    }
    return passwordLength;

}

// Let's ask the user what character types to include in their password (must include at least one type)
function charType() {
var upperChar = confirm("Click OK to include Upper case characters");
var lowerChar = confirm("click OK to include lower case characters");
var specialChar = confirm("Click OK to include special characters");
var numChar = confirm("Click OK to include numbers");

// Alert to notify user that at least one character type must be selected
  if (!(upperChar || lowerChar || specialChar || numChar)) {
  return alert("You must select at least one character type");
  }
}

// Function to generate password using the user-selected input(s)
function generatePassword(){
  chooseLength();
  console.log(passwordLength)
  charType();


var chars = "";
var password = "";
if (upperChar && lowerChar && specialChar && numChar){
  chars += upperCase + lowerCase + special +numeric;

}else if (upperChar  && numChar){
  chars += upperCase +numeric;

}else if (numChar && specialChar){
  chars += numeric + special;

}else if (upperChar && specialChar){
  chars += upperCase + special;

}else if (upperChar && lowerChar) {
  chars += upperCase + lowerCase;

}else if (lowerChar && numChar) {
  chars += lowerCase + numeric;

}else if (lowerChar && specialChar){
  chars += lowerCase + special;

}else if (upperChar){
  chars += upperCase;

}else if(numberChar){
  chars += numeric;

}else if (specialChar){
  chars += special;

}else{
  chars += lowerCase;
}

  for(var i = 0; i < passwordLength; i++){
    password += chars.charAt(Math.floor(Math.random() * chars.length));
  }
  return password;
}


// Write password to the #password input
function writePassword() {
  var password1 ="";
  password1 = generatePassword();
  var passwordText = document.querySelector("#password");

  passwordText.value = password1;

}

// Add event listener to generate button
generateBtn.addEventListener("click", writePassword);

ada chica is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

2

i think its because you define the variable inside a function and try to read it in other function

 function charType() {
    var upperChar = confirm("Click OK to include Upper case characters");
    var lowerChar = confirm("click OK to include lower case characters");
    var specialChar = confirm("Click OK to include special characters");
    var numChar = confirm("Click OK to include numbers");
    
    ...rest code
 }

you should try to move it outside to be a global variable like this

    var upperChar;
    var lowerChar;
    var specialChar;
    var numChar;

    function charType() {
        upperChar = confirm("Click OK to include Upper case characters");
        lowerChar = confirm("click OK to include lower case characters");
        specialChar = confirm("Click OK to include special characters");
        numChar = confirm("Click OK to include numbers");
        
        ...rest code
    }

There are some comments for you in the code.

That said, I know it’s probably just for practice but don’t use this password generator as it is not secure enough :).

// use const if possible
const upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lowerCase = "abcdefghijklmnopqrstuvwxyz";
const numeric = "0123456789";
const special = "!@#$%^&*()_-+={}[];:'`~<,>.?/|";
// avoid using "globals"
// var passwordLength;

function chooseLength() {
    passwordLength = prompt("How many characters would you like to include in your password?");
    if (passwordLength < 8) {
        alert("Password length must be at least 8 characters long");
        chooseLength();
    } else if (passwordLength > 128) {
        alert("Password length must be less than 129 characters");
        chooseLength();
    } else if (isNaN(passwordLength)) {
        alert("Password length must be a number between 8-128 characters");
        chooseLength();
    } else {
        alert("Please select what character types you would like to be included in your password.");
    }
    return passwordLength;
}

function generatePassword() {
    
    let password = "";
    // you define this booleans in charType() this wont work cause of scope
    const upperChar = confirm("Click OK to include Upper case characters");
    const lowerChar = confirm("click OK to include lower case characters");
    const specialChar = confirm("Click OK to include special characters");
    const numChar = confirm("Click OK to include numbers");

    if (!(upperChar || lowerChar || specialChar || numChar)) {
        return alert("You must select at least one character type");
    }
    
    let chars = "";
    this ifs can be simplyfied a lot
    if (upperChar) {
        chars += upperCase;
    }

    if (lowerChar) {
        chars += lowerCase;
    }

    if (specialChar) {
        chars += special;
    }

    if (numChar) {
        chars += numeric;
    }
    // this isn't really secure.. use https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
    for (let i = 0; i < passwordLength; i++) {
        password += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    
    return password;
}

function writePassword() {
    document.getElementById("password").innerHTML = generatePassword();
}

document.getElementById("generate").addEventListener("click", writePassword);
<button id="generate">Generate Password</button>
<div id="password"></div>

Not the answer you’re looking for? Browse other questions tagged javascript or ask your own question.