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);