我正在用 javascript 做練習,但遇到了一個問題。用戶應該選擇密碼中的字符長度,但是如果我在輸入中選擇 4 個字符,我會得到 3。為什么?
我的想法是選擇字符數并生成盡可能多的字符的密碼很有用。哪里錯了求幫助!
function generirajLozinku(pLength) {
var keyListAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
keyListInt = "123456789",
keyListSpec = "",
password = '@';
var len = Math.ceil(pLength / 2); // mijenanjem ovog broja mijenja se duzina lozinke
len = len - 1;
var lenSpec = pLength - 2 * len;
for (i = 0; i < len; i ) {
password = keyListAlpha.charAt(Math.floor(Math.random() * keyListAlpha.length));
password = keyListInt.charAt(Math.floor(Math.random() * keyListInt.length));
}
for (i = 0; i < lenSpec; i )
password = keyListSpec.charAt(Math.floor(Math.random() * keyListSpec.length));
password = password.split('').sort(function() {
return 0.5 - Math.random()
}).join('');
return password;
}
function myFunction() {
var x = document.getElementById("num").value;
document.getElementById("demo2").innerText = generirajLozinku(x);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Document</title>
</head>
<body>
<section>
<div>
<h1>Your strong password marker</h1>
<p>Password length</p>
<input type="number" name="izaberi boj karaktera!" id="num" min="4" max="10">
<br>
<button onclick="myFunction()">Generate</button>
<p id="demo2">Your password</p>
</div>
<div>
</div>
</section>
</body>
</html>
uj5u.com熱心網友回復:
它似乎不適用于偶數,即 4、6 等。您處理偶數與偶數的方式看起來不一樣。我為你做了一個更簡單的版本。
function generirajLozinku(pLength) {
var keyListAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789",
password = '@';
var len = pLength - 1;
for (i = 0; i < len; i ) {
password = keyListAlpha.charAt(Math.floor(Math.random() * keyListAlpha.length));
}
password = password.split('').sort(function() {
return 0.5 - Math.random()
}).join('');
return password;
}
function myFunction() {
var x = document.getElementById("num").value;
document.getElementById("demo2").innerText = generirajLozinku(x);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Document</title>
</head>
<body>
<section>
<div>
<h1>Your strong password marker</h1>
<p>Password length</p>
<input type="number" name="izaberi boj karaktera!" id="num" min="4" max="10">
<br>
<button onclick="myFunction()">Generate</button>
<p id="demo2">Your password</p>
</div>
<div>
</div>
</section>
</body>
</html>
uj5u.com熱心網友回復:
檢查您的代碼并對其進行注釋。pLength = 4 因為 keyListSpec = "" 所以下面的代碼
// keyListSpec is "", so keyListSpec.charAt always return ""
for (let i = 0; i < lenSpec; i )
password = keyListSpec.charAt(Math.floor(Math.random() * keyListSpec.length));
function generirajLozinku(pLength) {
var keyListAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
keyListInt = "123456789",
keyListSpec = "",
password = '@';
var len = Math.ceil(pLength / 2); // mijenanjem ovog broja mijenja se duzina lozinke
len = len - 1;
var lenSpec = pLength - 2 * len;
// if pLength = 4 , len is 1 and lenSpec is 2
// here password is "@"
for (let i = 0; i < len; i ) {
password = keyListAlpha.charAt(Math.floor(Math.random() * keyListAlpha.length));
password = keyListInt.charAt(Math.floor(Math.random() * keyListInt.length));
}
// if pLength = 4 , len is 1 and lenSpec is 2
// here password length is 3 "@xx"
console.log("keyListSpec", keyListSpec);
// keyListSpec is "", so keyListSpec.charAt always return ""
for (let i = 0; i < lenSpec; i )
password = keyListSpec.charAt(Math.floor(Math.random() * keyListSpec.length));
password = password.split('').sort(function() {
return 0.5 - Math.random()
}).join('');
return password;
}
uj5u.com熱心網友回復:
您的代碼不起作用的原因是這是您嘗試填充缺失字符的最后一個回圈:
for (i = 0; i < lenSpec; i )
password = keyListSpec.charAt(Math.floor(Math.random() * keyListSpec.length));
您從 中選擇一個隨機字符keyListSpec,但您定義keyListSpec為keyListSpec = "",因此沒有任何選擇。
你的代碼的設計選擇真的很奇怪。生成的密碼具有非常可預測的模式,這將大大減少可能的組合。密碼中字符的痛苦在那里無濟于事。除此之外,這sort(function() { return 0.5 - Math.random() })不是一個很好的洗牌方式。
另外Math.random()是不是一個好的選擇,當涉及到密碼生成。如果您正在尋找用 JavaScript 撰寫的密碼生成器,您應該查看例如Generate random password string with requirements in javascript,特別是在這個答案中。
uj5u.com熱心網友回復:
更新
@t.niese 是對的,如果他說隱藏真正的問題是個壞主意。正如我在第一個答案中所寫的那樣,您的代碼的某些部分對我來說似乎“很奇怪”。正如@Sprep 也正確指出的,偶數值和奇數值的處理。
這是作業代碼。受到@Sprep 回答的啟發。我只是縮短了一點。
function generirajLozinku(pLength) {
var keyListAlpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789',
password = '@';
var len = pLength - 1;
for (i = 0; i < len; i ) {
password = keyListAlpha.charAt(Math.floor(Math.random() * keyListAlpha.length));
}
return password;
}
老答
我已經你的一些代碼很奇怪。但是一個快速而簡單的解決方法是在回傳時插入這一行。return password.slice(0, len);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/341039.html
標籤:javascript
