简体   繁体   English

想要将用户输入放入 function

[英]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.如果您在链接下方直接看到 userDigits 输入,这就是我现在遇到的问题。 Not sure if the choseNo empty box choice is doing what I want it to do.不确定 chooseNo empty box 选项是否正在执行我想要的操作。 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.然后我将重复 userSpec、userLower 和 userUpper。 I have been trying to get this for the last 24 hours.在过去的 24 小时里,我一直在努力得到这个。 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.我只希望用户能够在下一个问题中输入 go,如果答案不是“是”或“不是小写或大写”,则强制返回。 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);

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

 function promptUser() {
  
}

I didn't add any prompt validation, or make it okay to click cancel, but here is an example of how to accomplish this idea.我没有添加任何提示验证,也没有让点击取消成为可能,但这里有一个如何实现这个想法的例子。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM