正在创建用于定义密码特征的功能吗?

问题描述 投票:1回答:2

我正在研究一个随机密码生成器,该生成器使用用户的提示根据用户定义的参数创建密码。到目前为止,我的提示已起作用并记录了正确的响应,但是我才刚刚开始学习JavaScript,并且对下一步的工作感到困惑。我不知道如何将可能的密码字符的变量以及用户响应的变量组合在一起。任何帮助表示赞赏!这是我到目前为止的代码:

var generateBtn = document.querySelector("#generate");
// Variables that could possibly be included, based on the user responses
var caps = ["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"]
var lower = ["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"]
var num = [0,1,2,3,4,5,6,7,8,9]
var spec = ['@', '%', '+', '', '/', "'", '!', '#', '$', '^', '?', ':', ',', ')', '(', '}', '{', ']', '[', '~', '-', '_', '.']
// Write password to the #password input

function generatePassword () {
  //Attributing variables based on user responses
var pwLength; 
 pwLength = prompt ("How many characters in your password? Please choose between 8-24.")
  if ((pwLength < 24) && (pwLength > 8)) {
    console.log (pwLength);
  }
  else {
    alert ("Please use characters between 8-24.");
    return false;
  };
var pwCaps 
  pwCaps = confirm("Would you like to include uppercase letters?")
  if (confirm){
    console.log (pwCaps);
};
var pwSpec 
  pwSpec = confirm("Would you like to include special characters?")
  if (confirm){
    console.log (pwSpec);
  };
  //Creating the length, case style, and character inclusion of the password based on the above variables
javascript variables passwords generator prompt
2个回答
0
投票

您可以使用此

function checkPassStrength(pass){

    let passwordVariations = {
        length: pass.length >= 8,
        digits: /\d/.test(pass),
        lower: /[a-z]/.test(pass),
        upper: /[A-Z]/.test(pass),
        nonWords: /\W/.test(pass),
        strength: 'Weak',
        progress: 10
    }

    let variationCount = 0;
    for (var check in passwordVariations) {
        variationCount += (passwordVariations[check] === true) ? 1 : 0;
    }

    if (variationCount === 5) {
        passwordVariations.progress = variationCount * 20
        passwordVariations.strength = "Excellent"
        return passwordVariations;
    }
    if (variationCount === 4) {
        passwordVariations.progress = variationCount * 20
        passwordVariations.strength = "Good"
        return passwordVariations;
    }
    if (variationCount === 3) {
        passwordVariations.progress = variationCount * 20
        passwordVariations.strength = "Average"
        return passwordVariations;
    }
    if (variationCount <= 2) {
        passwordVariations.progress = variationCount * 20
        passwordVariations.strength = "Weak"
        return passwordVariations;
    }
}

let result = checkPassStrength('Pass@123')
console.log(result)

0
投票

首先,您想允许所有字符都包含在密码中,所以您只需要将它们与Array#concat组合在一起,或仅使用Array#concat。然后,您只需要从最终数组中随机选择即可。

所以结尾看起来像这样:

但是您想从所需的字符集中获取项,因此您可以使用三元运算符将其作为条件并置。我们可以简单地将真实值或虚假值传递给我们先前实现的函数,然后确定是否要包含每组字符。通过为我们的每个参数提供默认的var caps = ["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"] var lower = ["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"] var num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] var spec = ['@', '%', '+', '', '/', "'", '!', '#', '$', '^', '?', ':', ',', ')', '(', '}', '{', ']', '[', '~', '-', '_', '.'] allowedCharacters = [...lower, ...num, ...caps, ...spec] function createPassword(length) { var password = ''; for (var i = 0; i < length; i++) { password += allowedCharacters[Math.floor(Math.random() * allowedCharacters.length)] } return password; } console.log(createPassword(10)) console.log(createPassword(5)) console.log(createPassword(12))值,我默认允许所有字符集。

所以您的最终代码应该是这样的:

true

NOTE:顺便说一句,实现并不是最好和最聪明的方法,因此,它的存在只是为了简单起见,而为了使您看清如何处理自己的情况,这里有更多说明。

© www.soinside.com 2019 - 2024. All rights reserved.