Skip to content
Advertisement

Alert windows for second function not popping up

I’m working on an assignment where I have to write a basic program that generates a random password based on certain criteria. I have written the following snippet of code to receive input from the user regarding the minimum and maximum values of the desired password length.

`

function writePassword() {
  
  var passwordText = document.querySelector("#password");
  passwordText.value = password;

  function exit(){ 
    return;
  }

  function numbersOnly(num){
    return /^[0-9]+$/.test(num); //checks if input only contains numbers
  }

  function getPassMin() {
  let passMin = prompt("Input the minimum length of your password in characters (If blank, defaults to minimum length of 8):");

  if (passMin < 8 && passMin !== "" && passMin !== null) { //checks if value is at least 8, reprompts if not
    alert("Sorry, the password must be at least 8 characters.");
    getPassMin();
  } else if (passMin > 128) { //checks if value is not more than 128, reprompts if not
    alert("Sorry, the password cannot exceed 128 characters.");
    getPassMin();
  } else if (passMin == "" || (numbersOnly(passMin) === false && passMin !== null)) { 
    alert("Please input a valid number in numeric format.");
    getPassMin();
  } else if (passMin === null){ //terminates program if cancel is pressed 
    writePassword.exit();
  } 
}
  
function getPassMax() {
  let passMax = prompt("Input the minimum length of your password in characters (If blank, defaults to maximum length of 128):");

  if (passMax > 128 && passMax!== "" && passMax !== null) { //checks if value is not more than 128, reprompts if not
    alert("Sorry, the password cannot exceed 128 characters.");
    getPassMax();
  } else if (passMax < 8) { //checks if value is at least 8, reprompts if not
    alert("Sorry, the password must be at least 8 characters.");
    getPassMax();
  } else if (passMax == "" || (numbersOnly(passMax) === false && passMax !== null)) { //checks if value is not blank and does not contain any non-numeric characters, reprompts if not
    alert("Please input a valid number in numeric format.");
    getPassMax();
  } else if (passMax < passMin){ //checks if value of getPassMax is not less than getPassMin, reprompts if not
    alert("Maximum value cannot be less than minimum length. You specified the minimum length as " + passMin + " characters.");
    getPassMax();
  } else if (passMax === null){ //terminates program if cancel is pressed 
    writePassword.exit();  
  }


}
getPassMin();
getPassMax();
}

`

The prompts in the getPassMin() method all work as expected, however the getPassMax() method only returns the proper prompt if the input value is either greater than 128 or less than 8, i.e the first 2 conditions. All the others simply result in closing the dialog window. I am not currently receiving any errors at any point during execution. Am I missing something?

I have tried copying and reusing the code from the getPassMin() method, and I have also tried re-writing the code from scratch. No success with either.

Advertisement

Answer

Your method getPassMax() cannot see the variable passMin which is causing an error. You could declare the variable passMin outside the scope of both methods, set the value inside getPassMin() and then it can be read inside getPassMax().

Also, be careful. When you’ve checked the value from the prompt is not null or empty (and against your number pattern or use if (!isNaN(value)) { ... }) then you should convert the value to a number. Currently you are comparing numbers inside strings which won’t give you correct results.

You can simply use let value = Number(input); and input will be converted to a number. Now your number comparisons will be correct.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement