你好堆栈溢出!
这是我第一次在网站上发帖,所以请坦白我和我的问题。我的班级的任务是使用 JavaScript 单独创建密码生成器。谢天谢地,我已经让大部分应用程序正常运行,但我遇到了一个问题。
示例: 用户选择在密码中包含 8 个字符,并选择包含特殊字符、小写字符和大写字符。有时生成密码时,它不会包含所有字符选择。(有时它会生成一个包含特殊字符和大写字符的密码,但没有一个小写字符)。
我已经完成了这个任务一分钟,但我的目标是了解我可以做些什么来解决这个问题并完成这个应用程序。我正在考虑可能删除 passwordOptions 对象并将每个选项变成自己的数组,您的想法是什么?
非常感谢您的任何建议!:D
// passwordOptions contains all necessary string data needed to generate the password
const passwordOptions = {
num: "1234567890",
specialChar: "!@#$%&'()*+,^-./:;<=>?[]_`{~}|",
lowerCase: "abcdefghijklmnopqrstuvwxyz",
upperCase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
};
document.getElementById('generate').addEventListener('click', function() {
alert(generatePassword());
});
// Executes when button is clicked
let generatePassword = function() {
// initial state for password information
let passInfo = "";
// ask user for the length of their password
let characterAmount = window.prompt("Enter the amount of characters you want for your password. NOTE: Must be between 8-128 characters");
// If the character length doesn't match requirements, alert the user
if (characterAmount >= 8 && characterAmount < 129) {
// ask if user wants to include integers
let getInteger = window.confirm("Would you like to include NUMBERS?");
// if user wants to include numbers
if (getInteger) {
// add numerical characters to password data
passInfo = passInfo + passwordOptions.num;
};
// ask if user wants to include special characters
let getSpecialCharacters = window.confirm("Would you like to include SPECIAL characters?");
// if user wants to include special characters
if (getSpecialCharacters) {
// add special characters to password data
passInfo = passInfo + passwordOptions.specialChar;
};
// ask if user wants to include lowercase characters
let getLowerCase = window.confirm("Would you like to include LOWERCASE characters?");
// if user wants to include lowercase characters
if (getLowerCase) {
// add lowercase characters to password data
passInfo = passInfo + passwordOptions.lowerCase;
};
// ask if user wants to include uppercase characters
let getUpperCase = window.confirm("Would you like to include UPPERCASE characters?");
// if user wants to include uppercase characters
if (getUpperCase) {
// add uppercase characters to password data
passInfo = passInfo + passwordOptions.upperCase;
};
// ensure user chooses at least one option
if (getInteger !=true && getSpecialCharacters !=true && getLowerCase !=true && getUpperCase !=true) {
// notify user needs to select at least one option
window.alert("You need to select at least one option, please try again!");
// return user back to their questions
return generatePassword();
};
// randomPassword is an empty string that the for loop will pass information in
let randomPassword = "";
// for loop grabs characterAmount to use
for (let i = 0; i < characterAmount; i++) {
//passInfo connects to charAt that uses both Math.floor and random to take the length of passInfo and randomize the results
randomPassword += passInfo[Math.floor(Math.random() * passInfo.length)];
};
// return password results
return randomPassword;
}
// if user's response is invalid
else {
// alert user
window.alert("You need to provide a valid length!");
// return user back to their questions
/* Removed for testing purposes to break the endless loop. */
// return generatePassword();
}
};
<button id="generate">Run</button>