我正在做一个密码生成器项目,为我的字符选项实现ascii码,我很难从数组中获取字符串。任何帮助都将不胜感激。我已经运行了几个测试,似乎我的问题是for循环和从我拥有的内容中获取字符串。对不起,如果这是一个愚蠢的问题。对这一点仍然非常陌生,尤其是javascript。非常感谢!!
var generateBtn = document.querySelector("#generate");
function writePassword() {
var password = generatePassword();
var passwordText = document.querySelector('#password');
passwordText.value = password;
}
function generatePassword() {
userpassword = "";
passwordCharacters = "";
let passwordlength = prompt("Select your desired password length");
if (passwordlength < 8 || passwordlength > 128 || passwordlength == isNaN) {
alert("Password length must be between 8 and 128 characters and numerical.");
return null;
}
var includespecial = confirm('Would you like to include special characters?');
var includeupper = confirm('Would you like to include uppercase characters?');
var includelower = confirm('Would you like to include lowercase characters?');
var includenumbers= confirm('Would you like to include numbers?');
var uppercasecharcodes = arrayFromLowToHigh(65, 90);
var lowercasecharcodes = arrayFromLowToHigh(97, 122);
var numbercharcodes = arrayFromLowToHigh(48, 57);
var specialcharcodes = arrayFromLowToHigh(33, 47).concat(
arrayFromLowToHigh(58, 64)
).concat(
arrayFromLowToHigh(91, 96)
).concat(
arrayFromLowToHigh(123, 126)
);
if (!includelower && !includenumbers && !includeupper && !includespecial) {
alert('You must choose atleast one of the options!');
return null;
}
// MERGE section where all options are compiled
if (includespecial) {
passwordCharacters += specialcharcodes;
}if (includeupper) {
passwordCharacters += uppercasecharcodes;
}if (includelower) {
passwordCharacters += lowercasecharcodes;
}if (includenumbers) {
passwordCharacters += numbercharcodes;
}
for (var i = 0; i < passwordlength; i++) {
userpassword += passwordCharacters[Math.floor(Math.random() * passwordlength)];
userpassword.push(String.fromCharCode(passwordCharacters));
}
return userpassword;
}
generateBtn.addEventListener("click",writePassword);
// FUNCTION for above arrayFromLowToHigh
function arrayFromLowToHigh(low, high) {
const array = []
for (let i = low; i <= high; i++) {
array.push(i)
}
return array
}
2条答案
按热度按时间eeq64g8w1#
我修正了你代码的一些部分,这是最后的代码,探索它,告诉我发生了什么,谢谢!
nfeuvbwi2#
去除
userpassword.push(String.fromCharCode(passwordCharacters));
因为userpassword不是数组,所以woule循环没有什么意义。