Search code examples
javascriptloopswhile-looppromptconfirm

How can I create a loop for a prompt, when the answer is unacceptable for a criteria?


so I've been working on a password generator and I have it working aside from 2 things. The password must be within 8-128 characters long. I have a prompt that asks the user how long they would like the password to be and then a few other prompts about including symbols, uppercase, lowercase, and numbers. However, when the user answers with anything bellow 8 or above 128 I would like an alert to say "password must be at least 8 characters long and shorter than 128" then loop back to the prompt asking them again how long they would like the password to be.

I can not for the life of me figure out how to do this. the obvious solution to me was a while loop however I am not experienced in building those.

I also want a minimum of one character type selected before the "confirm" system ends and to loop back to the beginning of the confirms for upper, lower, symbols, and numbers so the user has to pick at least one.

any help would be greatly appreciated.

thank you!

here is the code for the password length

var passLength;

while (passLength >= 129 || passLength <= 7) {
  passLength = parseInt(prompt('How many charcters would you like? (8-128)'))
    if (passLength <= 128 || passLength >= 8) {
        break;
    }
  alert("Password length must be 8 to 128 characters long!");
}

here is the code for the separate characters that I need at least one of to be selected.

var chosenCharactersArr = []
var passwordstring = ''

if (confirm('would you like Capital letters?')) {
  chosenCharactersArr.push(upperCase)
}

if (confirm('would you like to include lowercase letters?')) {
  chosenCharactersArr.push(lowerCase)
}

if (confirm('would you like to include numbers?')) {
  chosenCharactersArr.push(numbers)
}

if (confirm('would you like to include symbols?')) {
  chosenCharactersArr.push(symbols)
}

Solution

  • You have an error with your length check. You cannot use || in this case because you want to check for the lower- and upper limit to be both valid, therefore &&.

    For the question cycle, you can use a while (true) loop that you can break once you are done with the questions i.e. config in your case.

    I use an object to store the config values with defaults. You could also use an array. However, you have to manage the indices somehow.

    const TYPE_NUMBER = 0;
    const TYPE_BOOLEAN = 1;
    const TYPE_STRING = 2;
    
    const config = {
      len: 8,
      upc: false,
      loc: false,
      num: false,
      sym: false
    };
    
    while (true) {
      const len = ask('How many charcters would you like? (8-128)', TYPE_NUMBER);
      if (len <= 128 && len >= 8) {
        config.len = len; break;
      } else {
        alert("Password must be 8 to 128 characters long!");
      }
    }
    
    const upc = ask('Would you like to use capital letters?', TYPE_BOOLEAN);
    if (upc) { config.upc = upc; }
    
    // ... repeat the same for the other questions ...
    
    console.log(config);
    
    function ask(msg, type) {
      let result;
      if (type === TYPE_BOOLEAN) {
        result = confirm(msg);
      } else if (type === TYPE_NUMBER) {
        result = parseInt(prompt(msg));
      } else if (type === TYPE_STRING) {
        result = prompt(msg);
      } else {
        result = "";
      }
      return result;
    }