【解決方法】同じ数字または文字のみを生成します

[ad_1]

ランダムなパスワードを生成しようとしていますが、常に同じ番号が出力されます?!?

const characters = ["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","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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","~","`","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","}","]",",","|",":",";","<",">",".","?",
"/"];
let passElOne = document.getElementById("pass1");
let passElTwo = document.getElementById("pass2");

function generatePass()
{
   
        let randomChar = Math.floor(Math.random() * characters.length)
        let newChar = characters[randomChar]
       
    
        let randomPassword = ""
        for(i = 0; i < 15; i++)
        {
            randomPassword += newChar;
        }
        passElOne.textContent = randomPassword;
   
}

私が試したこと:

私はこの言語を学んでいるので、JavaScriptのスキルはかなり限られています.if else条件を書くことだけを試みていますが、うまくいきません

解決策 1

JavaScript
for(i = 0; i < 15; i++)
{
    randomPassword += newChar; // add the same character each time
}

パスワードに同じ文字を 15 個追加しています。 ループを一巡するたびに、新しいランダムな文字を生成する必要があります。

JavaScript
for(i = 0; i < 15; i++)
{
    let randomIndex = Math.floor(Math.random() * characters.length)
    randomPassword += characters[randomIndex];
}

[ad_2]

コメント

タイトルとURLをコピーしました