Wanted to put user input into function


I am wanting to put my user prompt inputs into my password generator. So far everything else works except for the user inputs. The only one I have working so far is length. If you see the userDigits input directly below links that is where I am getting stuck now. Not sure if the choseNo empty box choice is doing what I want it to do. I want them to type yes or no and get digits or not in there chosen length of password. Then I will repeat for userSpec, userLower, and userUpper. I have been trying to get this for the last 24 hours. The only help I can find is for confirm prompts and check boxes. I just want the user to be able to type them in and go on to next question and force a loop back if the answer is not yes or no lowercase or capital. Then finally I want to be able to put those into the password itself. All help is appreciated. Thank you.

    [![// Assignment Code
    var generateBtn = document.querySelector("#generate");
    
    const allowedDigits = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']; 
    const allowedUpperCase = ['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'];
    const allowedLowerCase = ['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'];
    const allowedSpecial = ['!', '@' , '#', '$', '%', '^', '&', '*', '?'];
    const choseNo = [];
    
    function getRandomCharacter(array) {
      return array[Math.floor(Math.random() * array.length)];
    }
    
    
    function generatePassword(length, allowedCharacterSets) {
      var password = "";
    
      for(let i = 0; i < allowedCharacterSets.length; ++i) {
    
      }
    
      for (var i = 0; i < length; i++) {
        password += getRandomCharacter(allowedCharacterSets[i % allowedCharacterSets.length]);
      }
      const UNIVERSAL_CHARACTER_SET = allowedCharacterSets.flat();
    
      for (let i = password.length;  i < length; ++i) {
        password += getRandomCharacter(UNIVERSAL_CHARACTER_SET);
      }
    
      return password; 
    
    }
    
    
    // Write password to the #password input
    function writePassword() {
    
        let isValidLength = false;
        let promptText = "How long would you like your password to be? Please choose between 8 and 128 characters."
        while (!isValidLength){
          var length = window.prompt(promptText);
          if (parseInt(length).toString() !== 'NaN'){
               if (length >= 8 && length <= 128){
                 isValidLength = true;
               } else {
                   promptText = 'Invalid option. Please enter a number between 8 and 128.';
               }
               
          }
        }
    
        var userDigits = window.prompt("Would you like to include numbers in your password? Type yes or no.");
          if (userDigits === "" || userDigits === null){
           var userDigits = window.prompt("Would you like to include numbers in your password? Type yes or no.");
            }
         userDigits = userDigits.toLocaleLowerCase();
          if (userDigits === "no") {
            userDigits = choseNo;
          }
            if (userDigits === "yes") {
              userDigits = allowedDigits;
              }
       
        var userSpec = window.prompt("Would you like to include special characters in your password? Type yes or no.");
        var userLower = window.prompt("Would you like to include lowercase letters in your password? Type yes or no.");
        var userUpper = window.prompt("Would you like to include uppercase letters in your password? Type yes or no.");
      
      var password = generatePassword(length, [allowedDigits, allowedSpecial, allowedLowerCase, allowedUpperCase]);
      var passwordText = document.querySelector("#password");
    
      passwordText.value = password;
    
    }
    
    // Add event listener to generate button
    generateBtn.addEventListener("click", writePassword);][1]][1]


  [1]: https://i.stack.imgur.com/36DGa.png

Source: JavaSript – Stack Overflow

October 3, 2021
Category : News
Tags: addeventlistener | function | javascript | prompt | user-input

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.