第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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

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

GCT1015 2023-03-18 16:39:02
我正在做一個(gè)編碼訓(xùn)練營,我們的目標(biāo)是設(shè)置一個(gè)密碼生成器,用戶可以選擇哪種類型的字符(小寫、大寫、數(shù)字和特殊字符)和長度,并為他們提供一個(gè)隨機(jī)的安全密碼。除了作業(yè)的一個(gè)重要部分,即生成的密碼必須包含用戶選擇的每個(gè)字符外,我能夠使它的所有方面都起作用。它目前是隨機(jī)抓取的,因此如果您選擇所有 4 個(gè)條件,它們將全部出現(xiàn)并不總是保證。我如何驗(yàn)證這一點(diǎn)?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 回答

?
料青山看我應(yīng)如是

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊

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


我已經(jīng)使用了您的代碼片段來生成一個(gè)獨(dú)立的函數(shù)makeSecurePassword(),該函數(shù)接受多個(gè)參數(shù):userLength, askLowerCase, askUpperCase, askNumerals, askSpecial。它返回請(qǐng)求的密碼userLength,僅包含請(qǐng)求的字符類型。它使用你的randChar()輔助函數(shù)。


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

}


查看完整回答
反對(duì) 回復(fù) 2023-03-18
  • 1 回答
  • 0 關(guān)注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)