如何在JavaScript中使用循环和条件语句,直到循环条件满足。

huangapple go评论65阅读模式
英文:

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 &lt; passwordLength) {
    if (optSelected.lowercase &amp;&amp; !lowercaseCounted) {
      console.log(&quot;execute random function lowercase&quot;);
      lowercaseCounted = true;
    } else {
      break;
    }

    if (optSelected.uppercase &amp;&amp; !uppercaseCounted) {
      console.log(&quot;execute random function uppercase&quot;);
      uppercaseCounted = true;
    }

    if (optSelected.numbers &amp;&amp; !numbersCounted) {
      console.log(&quot;execute random function numbers&quot;);
      numbersCounted = true;
    }

    if (optSelected.specials &amp;&amp; !specialsCounted) {
      console.log(&quot;execute random function specials&quot;);
      specialsCounted = true;
    }

    console.log(&quot;testing&quot;);

    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.

huangapple
  • 本文由 发表于 2023年1月9日 07:01:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定