【发布时间】:2025-12-05 23:25:01
【问题描述】:
我们必须生成一个 8-128 个字符之间的随机密码。用户应该为他们想要包含的字符(大写、小写、数字和特殊字符)获得一系列确定/取消选项。然后应该生成密码。我不确定我是否正确使用了 concat。尽管弹出确认窗口,但该按钮不会生成任何内容。不确定我是否只是贴错了标签,或者我完全不在了。
//Lowercase
var lowerCase = ['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'];
//Uppercase
var upperCase = ["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", ];
//Numeric
var numValue = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
//Special Characters
var spChar = ['@', '%', '+', '\\', '/', "'", '!', '#', '$', '^', '?', ':', ',', ')', '(', '}', '{', ']', '[', '~', '-', '_', '.'];
//var choices outside the if statement to concat Not sure if I should put this inside a function
var choices;
// Length of password, this is where the user selects how long their password should be(8 - 128 characters)
function generatePassword() {
var number = 0
while (number < 8 || number > 128) {
number = window.prompt("How long would you like your password to be? Enter a number between 8-128");
console.log(number);
}
//Where while loop ends
console.log("after ", number)
//Prompt for character types
var lcChoice = confirm("Would you like your password to contain lowercase characters?");
var ucChoice = confirm("How about uppercase characters?");
var numchoice = confirm("Would you like numbers?");
var spChoice = confirm("How about special characters? Like & or $?");
//If for negative on all 4 options - this doesn't return anything
if (!lcChoice && !ucChoice && !numchoice && spChoice) {
prompt = alert("You must choose a criteria!");
}
//Else if for all 4 character options
if (lcChoice && ucChoice && numchoice && spChoice) {
choices = spChar.concat(numValue, upperCase, lowerCase);
}
//Else if for 3 options (lc, uc, n) (uc, n, sp) (n, sp, lc) (sp, lc, uc)
else if (lcChoice && ucChoice && numchoice) {
choices = lcCase.concat(numValue, upperCase);
} else if (lcChoice && ucChoice && spChoice) {
choices = lowerCase.concat(spChar, upperCase);
} else if (lcChoice && spChoice && numchoice) {
choices = lowerCase.concat(numValue, spChar);
} else if (spChoice && ucChoice && numchoice) {
choices = spChar.concat(numValue, upperCase);
}
//Else if for 2 options (sp,n) (sp,lc) (sp, uc) (lc, n) (lc, uc) (n, uc)
else if (spChoice && numchoice) {
choices = spChar.concat(numValue);
} else if (spChoice && lcChoice) {
choices = spChar.concat(lowerCase);
} else if (spChoice && ucChoice) {
choices = spChar.concat(upperCase);
} else if (lcChoice && numchoice) {
choices = lowerCase.concat(numValue);
} else if (lcChoice && ucChoice) {
choices = lowerCase.concat(upperCase);
} else if (numchoice && ucChoice) {
choices = numValue.concat(upperCase);
}
//else if for one choice
else if (lcChoice) {
choices = lowerCase;
} else if (ucChoice) {
choices = upperCase;
} else if (numchoice) {
choices = numValue;
} else if (spChoice) {
choice = spChar;
}
var pwd = [];
for (i = 0; i === number; i++) {
pwd += choices[Math.floor(Math.random() * choices.length)]
}
return pwd;
}
//Get references to the #generate element
var generateBtn = document.querySelector("#generate");
//Write password to the #password input
function writePassword() {
var password = generatePassword();
var passwordText = document.querySelector("#password");
passwordText.value = password;
}
//Add event listener to generate button - This code was given to us
generateBtn.addEventListener("click", writePassword);
【问题讨论】:
-
很难判断您的代码中是否确实存在错误,或者您是否认为代码块中间会出现粗体文本。请修复您的代码....
-
choices = alert("You must choose a criteria!"); -
您可以将代码更改为 4 个 if 语句,没有理由进行所有组合。
标签: javascript arrays concatenation