想要将用户输入放入函数中

Wanted to put user input into function

我想将我的用户提示输入放入我的密码生成器中。到目前为止,除了用户输入外,其他一切都有效。到目前为止我唯一的工作是长度。如果您在链接下方直接看到 userDigits 输入,这就是我现在遇到的问题。不确定 chooseNo empty box 选项是否正在执行我想要的操作。我希望他们输入是或否,并在选择的密码长度中获取或不获取数字。然后我将重复 userSpec、userLower 和 userUpper。在过去的 24 小时里,我一直在努力得到这个。我能找到的唯一帮助是确认提示和复选框。我只希望用户能够输入它们并继续下一个问题,如果答案不是“是”或“不是小写或大写”,则强制返回。然后最后我希望能够将它们放入密码本身。感谢所有帮助。谢谢。

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

/**
 *  @return an array of allowed character arrays
 */

 function promptUser() {
  
}

我没有添加任何提示验证,也没有让它点击取消,但这里有一个如何实现这个想法的例子。

function main() {
  const parameters = getParameters();
  const pass = generatePass(parameters);
  console.log(`
    parameters = ${
      Object.entries(parameters)
      .reduce((a,[key, val]) => {
        return a + key + " : " + val + "\n"
      }, "\n\n")
    }
    pass is : ${pass}
    len is : ${pass.length}
  `);
}

function getParameters() {
  const lenRes = prompt("How long would you like your password to be? Please choose between 8 and 128 characters.");
  const len = parseInt(lenRes);

  const hasNumRes = prompt("Would you like to include numbers in your password? Type yes or no.");
  const hasNum = hasNumRes.toLowerCase().includes("y");

  const hasSpecRes = prompt("Would you like to include special characters in your password? Type yes or no.");
  const hasSpec = hasSpecRes.toLowerCase().includes("y");

  const hasLowerRes = prompt("Would you like to include lowercase letters in your password? Type yes or no.");
  const hasLower = hasLowerRes.toLowerCase().includes("y");

  const hasUpperRes = prompt("Would you like to include uppercase letters in your password? Type yes or no.");
  const hasUpper = hasUpperRes.toLowerCase().includes("y");

  return {len, hasNum, hasSpec, hasLower, hasUpper}
}

function generatePass(parameters) {
  const {len} = parameters;
  const charList = getCharList(parameters);
  let pass = "";
  for (let i = len; i --> 0 ;){
    pass += getRandomChar(charList);
  }
  return pass;
}

function getCharList({hasNum, hasSpec, hasLower, hasUpper}) {
  const allowedChars = [];
  const potentialDigits = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
  const potentialUpperCase = ['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 potentialLowerCase = ['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 potentialSpecial = ['!', '@' , '#', '$', '%', '^', '&', '*', '?'];
  if (hasNum) allowedChars.push(...potentialDigits);
  if (hasSpec) allowedChars.push(...potentialSpecial);
  if (hasLower) allowedChars.push(...potentialLowerCase);
  if (hasUpper) allowedChars.push(...potentialUpperCase);
  return allowedChars;
}

function getRandomChar(list) {
  const randChar = list[Math.floor(Math.random() * list.length)];
  return randChar
}

main();