3 回答
TA贡献1801条经验 获得超15个赞
我修复了你的代码并在代码片段中做了一些观察
// Assignment Code
var charString = ["ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklnmopqrstuvwxyz", "0123456789", "~!@#$%^&*()-_=+"];
var userInput = [];
var password = "";
var generateBtn = document.querySelector("#generate");
// Write password to the #password input
function writePassword() {
password = generatePassword();
var passwordText = document.querySelector("#password");
passwordText.value = password;
}
// Character length prompt
function generatePassword() {
var characterLength = 0
while ((characterLength < 8 || characterLength > 128) || Number.isInteger(characterLength) === false) {
characterLength = parseInt(prompt("How many characters would you like your password to be? (8-128)"));
}
var upper = false
var lower = false
var number = false
var symbol = false
while (!upper && !lower && !number && !symbol) {
// Uppercase pop up
upper = confirm("Click OK to confirm uppercase characters");
// Lowercase pop up
lower = confirm("Click OK to confirm lowercase characters");
// Numeric pop up
number = confirm("Click OK to confirm numeric characters");
// Special charcter pop up
symbol = confirm("Click OK to confirm special characters (~!@#$%^&*()-_=+)");
}
// userInput is an array, so you should push values into it.
if (upper) {
userInput.push(charString[0]);
}
if (lower) {
userInput.push(charString[1])
}
if (number) {
userInput.push(charString[2])
}
if (symbol) {
userInput.push(charString[3])
}
var password = "";
// join all array elements into a single one, and then split in order
// to get an array of characters
userInput = userInput.join("").split("");
// You should start your for statement at position 0, not 1
// and I guess it should be < than characterLength, not userInput.length
for (var i = 0; i < characterLength; i++) {
// Math.random is a function so you were missing the parenthesis Math.random()
// also Math.random() should be times userInput.length
// so they should be inside the same parenthesis
var index = (Math.floor(Math.random() * userInput.length));
password = password + userInput[index]
}
return password
}
// Add event listener to generate button
generateBtn.addEventListener("click", writePassword);
<button id="generate">Generate</button>
<br>
<input type="text" id="password">
TA贡献1836条经验 获得超4个赞
我对您的类型选择进行了一些更改以避免无限循环。你的问题是你的随机索引生成。就这样:
var charString = ["ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklnmopqrstuvwxyz", "0123456789",
"~!@#$%^&*()-_=+"];
var userInput = "";
var password = "";
var generateBtn = document.querySelector("#generate");
// Write password to the #password input
function writePassword() {
var passwordText = document.querySelector("#password");
password = generatePassword();
passwordText.value = ''; // Clear prev value
passwordText.value = password;
}
// Character length prompt
function generatePassword() {
var characterLength = 0
while ((characterLength < 8 || characterLength > 128) || Number.isInteger(characterLength) ===
false) {
characterLength = parseInt(prompt(`How many characters would you like your password to be? (8
128)`));
}
var upper = false
var lower = false
var number = false
var symbol = false
while (true) {
// Uppercase pop up
upper = confirm("Click OK to confirm uppercase characters");
// Lowercase pop up
lower = confirm("Click OK to confirm lowercase characters");
// Numeric pop up
number = confirm("Click OK to confirm numeric characters");
// Special charcter pop up
symbol = confirm("Click OK to confirm special characters (~!@#$%^&*()-_=+)");
break;
}
console.log('Using upper', upper);
console.log('Using lower', lower);
console.log('Using number', number);
console.log('Using symbol', symbol);
upper && (userInput += charString[0])
lower && (userInput += charString[1])
number && (userInput += charString[2])
symbol && (userInput += charString[3])
var password = "";
for (var i = 0; i < characterLength; i++) {
var index = Math.floor(Math.random() * userInput.length);
password += userInput[index]
}
return password
}
// Add event listener to generate button
generateBtn.addEventListener("click", writePassword);
<button id="generate">Generate</button>
<input id="password"/>
TA贡献1942条经验 获得超3个赞
改变
var index = (Math.floor(Math.random) * userInput);
到
var index = (Math.floor(Math.random() * userInput.length));
添加回答
举报