'How can I use window prompts/confirms and functions using math.floor/math.random to generate a password with an HTML button? (Javascript/HTML)

I'm trying to create a password generator using javascript and a generate password function tied to an HTML button with a 'click' event listener.

I have done all the work to get user-input, (created window prompt for password length, and window confirms for upper case, lower case, special characters, and numbers). I have created several functions which generate random outputs from a given range of the character code for each type.

How would I attach these randomizer functions into my generatePassword function so that they give back random values that correlate to the user-defined specifications and length?

I have tried creating an array of these functions, with no luck. I'm not sure how to iterate through the array at the given length, and everything I have tried has returned that these functions values are not defined. I have only been using javascript for about 2 weeks.

//Window Prompts for user/ Event Listeners

function userinputLength(){
  var passLength = Number(window.prompt("How many characters in length should your password be? Enter a number between 8 and 128", ""));
   //Comparing input from user to see if it's valid 
    if (passLength >= 129 || passLength <= 7 ) {
      window.alert("That's outside of the given range, try again")
      userinputLength()
    }
    
    else {
   
    if (typeof(passLength) === "number"){
      console.log(passLength);
      window.alert("You have chosen " + passLength);
    } else if (typeof(passLength) === "string", "char"){
      console.log("NOT A NUMBER");
      window.alert("That's not a number, try again.");
      userinputLength()
    }
  }
    
  
  
  //Create a variable containing prompt/alert/something window for user defined types of characters (Upper-case, Lower-Case, Special Characters(MAY NEED MORE THAN ONE for letters, case, numbers, special characters)
};
//function asking user for uppercase or not
function passCase1() { 
  var passCaseconfirm1 = window.confirm("Do you want Upper case characters?")
      if (passCaseconfirm1 === true) {
        window.alert("You have chosen to use upper case characters.")
      }else {
        window.alert("You have chosen NOT to use upper case characters.")
      }
    console.log(passCaseconfirm1);

};
// function asking user if they want lower case characters or not
function passCase2() { 
  var passCaseconfirm2 = window.confirm("Do you want lower case characters?")
      if (passCaseconfirm2 === true) {
        window.alert("You have chosen to use lower case characters.")
      }else {
        window.alert("You have chosen NOT to use lower case characters.")
      }
      //USE UPPERCASE ARRAY
    
    console.log(passCaseconfirm2);

};

//function asking user for special characters or not
function passChar(){
  var passCharconfirm = window.confirm("Do you want to use special characters or not?");
  if (passCharconfirm) {
    window.alert("You have chosen to use special characters");
    //USE SPECIAL CHARACTERS ARRAY
  } else {
    window.alert("You have chosen NOT to use special characters");
    //DONT USE SPECIAL CHARACTERS ARRAY
  }
  console.log(passCharconfirm);
}  
// Function asking user if they want random numbers or not
function passNum(){
  var passNumconfirm = window.confirm("Do you want to use numbers or not?");
  if (passNumconfirm) {
    window.alert("You have chosen to use number");
    //USE SPECIAL CHARACTERS ARRAY
  } else {
    window.alert("You have chosen NOT to use numbers");
    //DONT USE SPECIAL CHARACTERS ARRAY
  }
  console.log(passNumconfirm);
}  

//Functions to get random output from character code. 


function getRandomLower() {
  return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
}

function getRandomUpper() {
  return String.fromCharCode(Math.floor(Math.random() * 26) + 65);
}

function getRandomNumber() {
  return String.fromCharCode(Math.floor(Math.random() * 10) + 48);
}

function getRandomSymbol() {
  const symbols = "!@#$%^&*()_+|\={}[]>?<;'./,"
  return symbols[Math.floor(Math.random() *  symbols.length)];
}

//Generate Password function and associated function calls. 
function generatePassword() {
  userinputLength();
  passCase1();
  passCase2();
  passChar(); 
  passNum();
  //NEED MORE FUNCTION CALLS HERE THAT DETERMINE IF EACH CASE IS TRUE OR FALSE AND RETURNS VALUE BASED ON LENGTH
  };

// Get references to the #generate element
var generateBtn = document.querySelector("#generate");

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

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

//CALL FUNCTIONS


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source