一种检查字符串是否包含来自特定子字符串的字符的方法

我正在做一个编码训练营,我们的目标是设置一个密码生成器,用户可以选择哪种类型的字符(小写、大写、数字和特殊字符)和长度,并为他们提供一个随机的安全密码。

除了作业的一个重要部分,即生成的密码必须包含用户选择的每个字符外,我能够使它的所有方面都起作用。它目前是随机抓取的,因此如果您选择所有 4 个条件,它们将全部出现并不总是保证。我如何验证这一点?

const lowCaseArr = "abcdefghijklmnopqrstuvwxyz";

const upCaseArr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

const numeralArr = "1234567890";

const specialArr = "!@#$%^&*";


function getLength() {

    while (true) {

        var userLength = parseInt(prompt("How many numbers, between 8 and 128, would you like to use? (Enter 0 to cancel)"));

        if (userLength == 0) {

            return 0;

        } else if (userLength > 128 || userLength < 8) {

            alert("You must enter a number between 8-128.");

        } else if (userLength <= 128 && userLength >= 8) {

            alert("Great! Your have selected a password with " + userLength + " characters.");

            return userLength;

        }

    } 

}


function randChar(passwordCharacters) {

    return passwordCharacters.charAt(Math.floor(Math.random() * passwordCharacters.length));

}


function makePassword(userLength, passwordCharacters) { 

    var securePassword = "";

    for (i = 0; i < userLength; i++) {    

        securePassword += randChar(passwordCharacters);

    }

    return securePassword;

}


function generatePassword() {

    var userLength = getLength();

    if (userLength == 0) {

        return "User Cancelled Request";

    }



    var passwordCharacters = "";

    var askLowerCase = confirm("Would you like to include lower case characters? (a, b, c)");

    if (askLowerCase !== true) {

        alert("Got it. No lower case characters will be included.");

    } else {

        alert("Great! Your password will include lower case characters!");

        passwordCharacters += lowCaseArr;

    }

GCT1015
浏览 62回答 1
1回答

料青山看我应如是

与其在事后更改密码,不如通过确保密码满足所有约束从一开始就正确生成密码。我已经使用了您的代码片段来生成一个独立的函数makeSecurePassword(),该函数接受多个参数:userLength, askLowerCase, askUpperCase, askNumerals, askSpecial。它返回请求的密码userLength,仅包含请求的字符类型。它使用你的randChar()辅助函数。var securePassword = makeSecurePassword( 10, true, true, true, true );console.log(securePassword);// Return a random character from passwordCharacters:function randChar(passwordCharacters) {&nbsp; &nbsp; return passwordCharacters.charAt(Math.floor(Math.random() * passwordCharacters.length));}function makeSecurePassword( userLength, askLowerCase, askUpperCase, askNumerals, askSpecial ) {&nbsp; &nbsp; const lowCaseArr = "abcdefghijklmnopqrstuvwxyz";&nbsp; &nbsp; const upCaseArr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";&nbsp; &nbsp; const numeralArr = "1234567890";&nbsp; &nbsp; const specialArr = "!@#$%^&*";&nbsp; &nbsp; var password = [];&nbsp; &nbsp; // Decide which chars to consider:&nbsp; &nbsp; charArray = [];&nbsp; &nbsp; if ( askLowerCase ) {&nbsp; &nbsp; &nbsp; &nbsp; charArray.push( lowCaseArr );&nbsp; &nbsp; }&nbsp; &nbsp; if ( askUpperCase ) {&nbsp; &nbsp; &nbsp; &nbsp; charArray.push( upCaseArr );&nbsp; &nbsp; }&nbsp; &nbsp; if ( askNumerals ) {&nbsp; &nbsp; &nbsp; &nbsp; charArray.push( numeralArr );&nbsp; &nbsp; }&nbsp; &nbsp; if ( askSpecial ) {&nbsp; &nbsp; &nbsp; &nbsp; charArray.push( specialArr );&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; let x = 0; // index into charArray&nbsp; &nbsp; for ( var i=0; i < userLength; i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; var a = charArray[x]; // Which array of chars to look at&nbsp; &nbsp; &nbsp; &nbsp; // Insert at random spot:&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; password.splice( password.length, 1, randChar( a ) );&nbsp; &nbsp; &nbsp; &nbsp; // Pick next set of chars:&nbsp; &nbsp; &nbsp; &nbsp; if ( ++x >= charArray.length ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = 0; // Start with the first set of chars if we went past the end&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return password.join(''); // Create a string from the array of random chars}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript