我写/使用concat正确的密码生成器在Javascript?



我们必须生成一个8-128个字符之间的随机密码。用户应该为他们想要包含的字符(大写、小写、数字和特殊字符)获得一系列ok/cancel选项。然后应该生成密码。我不确定我是否正确使用了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);

我认为你没有从密码生成器中得到任何东西的原因是:

for (i = 0; i === number; i++) {
pwd += choices[Math.floor(Math.random() * choices.number)]
}

选择。Number未定义(这指的是choices对象的Number属性,该属性不存在)

请尝试将此更改为choices。长度(数组的长度)

您的代码中有几个错误,其中包括choices = spChoice.concat(numValue, upperCase, lowerCase);,它应该是spChar和类似的choices = lcChoice.concat(numValue, upperCase);。然后这里pwd += choices[Math.floor(Math.random() * choices.number)]没有choices.number,因为它是一个字符串。应该改成choices.length

//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 = lowerCase.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");
console.log(password)
passwordText.value = password;
}
//Add event listener to generate button - This code was given to us
generateBtn.addEventListener("click", writePassword);
<input type="text" id="password">
<button id="generate">Generate</button>

编辑完标签后,我需要将函数的长度或迭代改为I <数字。>

最新更新