gpt4 book ai didi

javascript - 我的密码生成器的输出低于预期

转载 作者:行者123 更新时间:2023-12-05 05:39:41 25 4
gpt4 key购买 nike

我正在尝试为编码训练营制作密码生成器,但我遇到了一个问题,该问题有 2 个结果,这两个结果都不是我想要的。 HTML 非常基础,我应该使用提示进行选择。我包含了我的代码,但删除了一些不必要的东西、其他 14 个 if-else 语句和一些变量。我正在提交带有丑陋格式和空格的项目,但仍然想知道我哪里出错了。这两个结果是

  1. 选择不会是唯一的,而是一遍又一遍地使用相同的字符
  2. 它看起来很草率,并在其中添加了空格
function randomLower() {
return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
}
//random uppercase from character code
function randomUpper() {
return String.fromCharCode(Math.floor(Math.random() * 26) + 65);
}
//random number from character code
function randomNumber() {
return String.fromCharCode(Math.floor(Math.random() * 10) + 48);
}
// random symbol from character code
function randomSymbol() {
let symbol = "!@#$%^&*()_-><[]}{";
return symbol[Math.floor(Math.random() * symbol.length)];
}

// 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
generateBtn.addEventListener("click", writePassword);

// function for generatePassword
function generatePassword() {
//Confirms # of characters needed
click = parseInt(prompt("How many characters?", "8-128"));
if (!click) {
alert("Input is needed");
//Ensures the character count isn't too high or low
} else if (click < 8 || click > 128) {
click = parseInt(prompt("Answer must be between 8 and 128"));
} else {
//The procedding few lines will confirm other variables
useNumbers = confirm("Include numbers?");
useLower = confirm("Include lowercase letters?");
useUpper = confirm("Include uppercase letters?");
useSymbol = confirm("Include special characters?");
}
//If all prompts are denied
if (!useLower && !useUpper && !useNumbers && !useSymbol) {
choices = alert("At least one option is needed");
//If all prompts are accepted
} else if (useLower && useUpper && useNumbers && useSymbol) {
choices = randomLower().concat(randomUpper, randomNumber, randomSymbol);
//code only makes repeating characters
//choices = randomLower().concat(randomUpper(), randomNumber(), randomSymbol())

//placeholder for an array for user choices
var pWord = [];
//for loop to randomize selection
for (let i = 0; i < click; i++) {
var selected = choices[Math.floor(Math.random() * choices.length)];
pWord.push(selected);
}

//.join will take all choices in the array pWord and turns it into a string
var pass = pWord.join("");
UserInput(pass);
return pass;
}
// This puts the new password into the textbox
function UserInput(pass) {
document.getElementById("password").value = pass;
}

最佳答案

  • 只有当用户选择所有选项时,您才设置选择
  • 当您设置choices 时,您不会调用concat() 参数中的函数。因此,您连接的是函数定义,而不是函数返回的随机字母。
  • 即使您修复它来调用函数,您也只会得到 4 个字符。您需要在生成每个字符的循环中调用 randomXXX 函数。

在我的代码中,我将 choices 设为一个函数数组,而不是字符数组。我根据提示有条件地将每个函数添加到数组中。然后循环首先选择一个随机函数,并调用它来获得该类型的随机字符。

// function for generatePassword
function generatePassword() {
//Confirms # of characters needed
let click = parseInt(prompt("How many characters?", "8-128"));
let choices = [];
if (!click) {
alert("Input is needed");
//Ensures the character count isn't too high or low
} else if (click < 8 || click > 128) {
click = parseInt(prompt("Answer must be between 8 and 128"));
} else {
//The procedding few lines will confirm other variables
if (confirm("Include numbers?")) {
choices.push(randomNumber);
}
if (confirm("Include lowercase letters?")) {
choices.push(randomLower);
}
if (confirm("Include uppercase letters?")) {
choices.push(randomUpper);
}
if (confirm("Include special characters?")) {
choices.push(randomSymbol);
}
}
//If all prompts are denied
if (choices.length == 0) {
alert("At least one option is needed");
return;
}

//placeholder for an array for user choices
var pWord = [];
//for loop to randomize selection
for (let i = 0; i < click; i++) {
let selected = choices[Math.floor(Math.random() * choices.length)];
pWord.push(selected());
}

//.join will take all choices in the array pWord and turns it into a string
var pass = pWord.join("");
return pass;
}

console.log(generatePassword());

function randomLower() {
return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
}
//random uppercase from character code
function randomUpper() {
return String.fromCharCode(Math.floor(Math.random() * 26) + 65);
}
//random number from character code
function randomNumber() {
return String.fromCharCode(Math.floor(Math.random() * 10) + 48);
}
// random symbol from character code
function randomSymbol() {
let symbol = "!@#$%^&*()_-><[]}{";
return symbol[Math.floor(Math.random() * symbol.length)];
}

关于javascript - 我的密码生成器的输出低于预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72625400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com