【问题标题】:Wanted to put user input into function想要将用户输入放入函数中
【发布时间】:2021-11-24 12:07:39
【问题描述】:

我想将我的用户提示输入放入我的密码生成器中。到目前为止,除了用户输入之外,其他一切都有效。到目前为止,我唯一的工作就是长度。如果您直接在链接下方看到 userDigits 输入,这就是我现在卡住的地方。不确定 selectedNo 空框选项是否正在做我想要做的事情。我希望他们输入是或否并在选择的密码长度中获取数字或不获取数字。然后我将重复 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() {
  
}

【问题讨论】:

  • 分配allowedDigits时为什么要转义方括号?
  • @asyn await 我想我修好了。只是一个糟糕的复制和粘贴。
  • 明白了。为什么你的循环 allowedCharacterSets.length 有一个空代码块?
  • @asyncawait 我不确定你的意思。这就是我的失落。我已经完成了 24 小时的 90%。我只想能够使用输入生成密码
  • 是否必须提示或者您可以使用 html 元素来获取他们的输入?

标签: javascript function user-input addeventlistener prompt


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多