我刚刚开始了一个编码训练营,对于我们的第三个作业,我们必须使用一些用户偏好编写一个随机密码生成器。 我写了我的代码,除了一点之外,一切都可以工作。当用户只选择具有数值时,会给出一个错误,其中我的选择字符池告诉我"pSelection.charAt 不是生成PWD的函数"。
考虑到该功能在所有其他情况下都能完美运行,这对我来说毫无意义。我尝试为失败的特定(否则如果(语句创建一个单独的数组。我尝试在"Char"发生冲突时交换变量名称。
如果有人可以看看并告诉我我做错了什么,我将不胜感激,因为我认为我明白发生了什么,但显然不是。
这是JavaScript - 下面是HTML
// Add event listener to the generate button
document.getElementById("generate").addEventListener("click", function(){
// Setting up the possible Characters
var lowercaseAlphabetChar = ("abcdefghijklmnopqrstuvwxyz");
var uppercaseAlphabetChar = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
var numericChar = (1234567890);
var specialChar = ("!@#$%^&*()?~/|:'><");
var password = "";
var pSelection = "";
// User Prompts
var length = prompt("How long is your Password? Choose between 8 and 128 Characters");
// Making sure User Chooses between 8 and 128 Characters
if (length < 8 || length > 128){
alert("Length has to be Between 8 and 128. Please Try Again")
return
}
var wantsLower = confirm("Do you want Lowercase Characters?")
var wantsUpper = confirm("Do you want Uppercase Characters?")
var wantsNumbers = confirm("Do you want Numerical Characters?")
var wantsSpecial = confirm("Do you want Special Characters?")
//Verification Points System - I allocate a point when an option is confirmed. if no points are allocated, user has not selected anything
var verify = 0;
if (wantsLower == true || wantsUpper == true || wantsNumbers == true || wantsSpecial == true) {
verify += 1;
}
if (verify == 0) {
alert("You have not selected a character Type, Please Try again.")
return
}
// Script will exit if not verified, if verified we Proceed to Grouping Password to User Needs.
function generatePWD() {
for (var i = 0; i <= length; i++) {
password = password + pSelection.charAt(Math.floor(Math.random() * Math.floor(pSelection.length - 1)));
}
}
// IF STATEMENTS for User PreferenceLowerCase
if (wantsLower && !wantsUpper && !wantsNumbers && !wantsSpecial) {
pSelection = lowercaseAlphabetChar;
generatePWD();
}
// LowerCase and UpperCase
else if (wantsLower && wantsUpper && !wantsNumbers && !wantsSpecial) {
pSelection = lowercaseAlphabetChar + uppercaseAlphabetChar;
generatePWD();
}
//LowerCase and UpperCase and Numbers
else if (wantsLower && wantsUpper && wantsNumbers && !wantsSpecial) {
pSelection = lowercaseAlphabetChar + uppercaseAlphabetChar + numericChar;
generatePWD();
}
// Everything
else if (wantsLower && wantsUpper && wantsNumbers && wantsSpecial) {
pSelection = lowercaseAlphabetChar + uppercaseAlphabetChar + numericChar + specialChar;
generatePWD();
}
//Lowercase and Numbers
else if (wantsLower && !wantsUpper && wantsNumbers && !wantsSpecial) {
pSelection = lowercaseAlphabetChar + numericChar;
generatePWD();
}
//Lowercase and Numbers and Special
else if (wantsLower && !wantsUpper && wantsNumbers && wantsSpecial) {
pSelection = lowercaseAlphabetChar + numericChar + specialChar;
generatePWD();
}
// Lowercase and Special
else if (wantsLower && !wantsUpper && !wantsNumbers && wantsSpecial) {
pSelection = lowercaseAlphabetChar + specialChar;
generatePWD();
}
// Uppercase
else if (!wantsLower && wantsUpper && !wantsNumbers && !wantsSpecial) {
pSelection = uppercaseAlphabetChar;
generatePWD();
}
// UpperCase and Numbers
else if (!wantsLower && wantsUpper && wantsNumbers && !wantsSpecial) {
pSelection = uppercaseAlphabetChar + numericChar;
generatePWD();
}
// Uppercase and Numbers and Special
else if (!wantsLower && wantsUpper && wantsNumbers && wantsSpecial) {
pSelection = uppercaseAlphabetChar + numericChar + specialChar;
generatePWD();
}
// Uppercase and Special
else if (!wantsLower && wantsUpper && !wantsNumbers && wantsSpecial) {
pSelection = uppercaseAlphabetChar + specialChar;
generatePWD();
}
// Numbers
else if (!wantsLower && !wantsUpper && wantsNumbers && !wantsSpecial) {
pSelection = numericChar;
generatePWD();
}
// Numbers and Special
else if (!wantsLower && !wantsUpper && wantsNumbers && wantsSpecial) {
pSelection = numericChar + specialChar;
generatePWD();
}
// Special
else if (!wantsLower && !wantsUpper && !wantsNumbers && wantsSpecial) {
pSelection = specialChar;
generatePWD();
}
document.getElementById("password").innerHTML = password;
console.log(password);
});
.HTML
<!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>Password Generator</title>
<link rel="stylesheet" href="assets/style.css" />
</head>
<body>
<div class="wrapper">
<header>
<h1>Password Generator</h1>
</header>
<div class="card">
<div class="card-header">
<h2>Generate a Password</h2>
</div>
<div class="card-body">
<textarea
readonly
id="password"
placeholder="Your Secure Password"
aria-label="Generated Password"
></textarea>
</div>
<div class="card-footer">
<button id="generate" class="btn">Generate Password</button>
</div>
</div>
</div>
<script src="assets/script.js"></script>
</body>
</html>
numericChar
需要是一个字符串,即"1234567890"
- 照原样,你已经用一个数字初始化了它。
如果将变量的声明更改为
var numericChar = "1234567890";
然后它应该:)工作