英文:
How to use a loop with if statements to execute until the loops condition is met in javascript
问题
感谢您提前解答这个难题!
我有一个生成密码的函数,它接受用户通过提示和确认所选择的一些选项,其中一个提示返回密码长度,所有这些选项都存储在一个选项对象中,我可以在该函数中使用点表示法访问该对象。
我需要生成一堆由用户选择确定的字符 - 它只能是用户在提示中输入的长度。
在这种情况下,我决定使用一个循环,当满足密码长度条件时,在每次迭代时,if语句的主体应执行一个getRandom函数,该函数将以其作为参数传递的数组返回单个随机字符。
然而,在测试我的代码时,我无法使if语句仅执行用户输入的密码长度次数。
我如何使用while循环或其他循环实现这一点。
function generatePassword() {
const optSelected = getPasswordOptions();
let generatedPassArr = [];
const passwordLength = optSelected.passwordLength;
let counter = 0;
let lowercaseCounted;
let uppercaseCounted;
let numbersCounted;
let specialsCounted;
while (counter < passwordLength) {
if (optSelected.lowercase && !lowercaseCounted) {
console.log("执行随机小写字符函数");
lowercaseCounted = true;
} else {
break;
}
if (optSelected.uppercase && !uppercaseCounted) {
console.log("执行随机大写字符函数");
uppercaseCounted = true;
}
if (optSelected.numbers && !numbersCounted) {
console.log("执行随机数字字符函数");
numbersCounted = true;
}
if (optSelected.specials && !specialsCounted) {
console.log("执行随机特殊字符函数");
specialsCounted = true;
}
console.log("测试中");
counter++;
}
console.log(generatedPassArr);
}
英文:
Appreciate any answers to this conumdrum in advance !
I have a function to generate password which takes in a number of options chosen by a user via prompts and confirms - one of these prompts returns a password length and all these options are stored within an options object i get access to using dot notation within this function.
I need to generate a bunch of characters which have been determined by the users choices - it needs to be only the length the user has entered in the prompt.
In this case i've decided to have a loop where a password lengths condition is met and at each iteration the body of the if statement should execute a getRandom function which will return a single random character from array passed to it as an argument to function.
However on testing my code - i can not get the if statements to only execute only the number of times the user has entered as the password length.
How do i achieve this with a while loop or any other loop.
function generatePassword() {
const optSelected = getPasswordOptions();
let generatedPassArr = [];
const passwordLength = optSelected.passwordLength;
let counter = 0;
let lowercaseCounted;
let uppercaseCounted;
let numbersCounted;
let specialsCounted;
while (counter < passwordLength) {
if (optSelected.lowercase && !lowercaseCounted) {
console.log("execute random function lowercase");
lowercaseCounted = true;
} else {
break;
}
if (optSelected.uppercase && !uppercaseCounted) {
console.log("execute random function uppercase");
uppercaseCounted = true;
}
if (optSelected.numbers && !numbersCounted) {
console.log("execute random function numbers");
numbersCounted = true;
}
if (optSelected.specials && !specialsCounted) {
console.log("execute random function specials");
specialsCounted = true;
}
console.log("testing");
counter++;
}
console.log(generatedPassArr);
}
As you can see i have attempted to create variables so that each if statement does not repeat continously in a block - rather i need atleast the if block to execute once - if the object property exists true, until the loop reaches the password length.
答案1
得分: 1
除非你有特定需要在这里使用while
循环(即这是一个任务),你考虑过使用正则表达式来测试吗?
对于每个密码条件,使用一个单独的正则表达式,我认为你可以完全避免使用while
循环。
英文:
Unless you have a specific need to use a while
loop here (i.e. this is an assignment) have you considered using regex to test this instead?
With a singular regex for each password criteria, I think you could avoid the use of the while
loop altogether.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论