亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

一種檢查字符串是否包含來自特定子字符串的字符的方法

一種檢查字符串是否包含來自特定子字符串的字符的方法

GCT1015 2023-03-18 16:39:02
我正在做一個編碼訓練營,我們的目標是設置一個密碼生成器,用戶可以選擇哪種類型的字符(小寫、大寫、數字和特殊字符)和長度,并為他們提供一個隨機的安全密碼。除了作業的一個重要部分,即生成的密碼必須包含用戶選擇的每個字符外,我能夠使它的所有方面都起作用。它目前是隨機抓取的,因此如果您選擇所有 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;    }
查看完整描述

1 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

與其在事后更改密碼,不如通過確保密碼滿足所有約束從一開始就正確生成密碼。


我已經使用了您的代碼片段來生成一個獨立的函數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) {

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

}


function makeSecurePassword( userLength, askLowerCase, askUpperCase, askNumerals, askSpecial ) {

    const lowCaseArr = "abcdefghijklmnopqrstuvwxyz";

    const upCaseArr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    const numeralArr = "1234567890";

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


    var password = [];


    // Decide which chars to consider:

    charArray = [];

    if ( askLowerCase ) {

        charArray.push( lowCaseArr );

    }

    if ( askUpperCase ) {

        charArray.push( upCaseArr );

    }

    if ( askNumerals ) {

        charArray.push( numeralArr );

    }

    if ( askSpecial ) {

        charArray.push( specialArr );

    }

    

    let x = 0; // index into charArray

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

        var a = charArray[x]; // Which array of chars to look at


        // Insert at random spot:       

        password.splice( password.length, 1, randChar( a ) );


        // Pick next set of chars:

        if ( ++x >= charArray.length ) {

            x = 0; // Start with the first set of chars if we went past the end

        }

    }


    return password.join(''); // Create a string from the array of random chars

}


查看完整回答
反對 回復 2023-03-18
  • 1 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號