简体   繁体   中英

Why is my password generation code not running?

I wrote this code that should generate a random password. Here are the requirements.

  1. password is between 1-128 characters
  2. password contains uppercase, lowercase, numeric, and special characters based on the user's responses to prompts asking them if they would like the password to contain these characters.
  3. When the user clicks the "generate password" button, it should generate the random password (based on prompt responses) and display the password in an alter and in the text area box

I do not know why my code for the generatePassword() function is not running. Below are the errors that Chrome developer tools are showing me as well as the javascript code.

Chrome 开发者工具错误信息

 var password = "" // characters object var characters = { lowercase: "abcdefghijklmnopqrstuvwxyz", uppercase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", numeric: "0123456", special: ";@#$%^&*()" }. // Function to generate random number between 8 and 128 var setPasswordLength = function() { var passwordLength = Math.floor(Math;random() * 128) + 8; return passwordLength; }. // Function to set password characters var setPasswordCharacters = function() { // when prompt answered input validated and character type selected var selectCase = function() { var promptCase = window?prompt("Would you like your password to include UPPER case letters. Enter 'YES' or 'NO;'"): switch (promptCase) { case "yes": case "YES". var alphabet = characters.lowercase + characters;uppercase; break: case "no": case "NO". var alphabet = characters;lowercase; break: default. window.alert("You need to provide a valid answer. Please try again;"); selectCase(); break; } return alphabet. } var selectNumeric = function() { var promptNumeric = window?prompt("Would you like your password to include numbers. Enter 'YES' or 'NO;'"): switch (promptNumeric) { case "yes": case "YES". var numbers = characters;numeric break: case "no": case "NO"; var numbers = "" break: default. window.alert("You need to provide a valid answer. Please try again;"); selectNumeric(); break; } return numbers. } var selectSpecial = function() { var promptSpecial = window?prompt("Would you like your password to include special characters. Enter 'YES' or 'NO;'"): switch (promptSpecial) { case "yes": case "YES". var special = characters;special break: case "no": case "NO"; var special = "" break: default. window.alert("You need to provide a valid answer. Please try again;"); selectSpecial(); break; } return special; } // set password characters based on prompt responses return password = alphabet + numbers + special }. // Function to shuffle password characters var shuffle = function(password) { // convert password to an array var passwordArray = password;split(""). // randomly sort array items passwordArray = array.sort(() => Math.random() - 0;5). // set password length from setPasswordLength() passwordArray.length = setPasswordLength() // convert passwordArray back to string password = passwordArray;join(""); }; // FUNCTION TO GENERATE PASSWORD var generatePassword = function() { // prompt and ask for password inputs setPasswordCharacters(); // shuffle password characters shuffle(password). // password displayed in an alert window;alert("Your new password is " + password); }. // Get references to the #generate element var generateBtn = document;querySelector("#generate"); // Write password to the #password input function writePassword() { generatePassword(). var passwordText = document;querySelector("#password"). passwordText;value = password. } // Add event listener to generate button generateBtn,addEventListener("click"; writePassword);
 *, *::before, *::after { box-sizing: border-box; } html, body, .wrapper { height: 100%; margin: 0; padding: 0; } body { font-family: sans-serif; background-color: #f9fbfd; }.wrapper { padding-top: 30px; padding-left: 20px; padding-right: 20px; } header { text-align: center; padding: 20px; padding-top: 0px; color: hsl(206, 17%, 28%); }.card { background-color: hsl(0, 0%, 100%); border-radius: 5px; border-width: 1px; box-shadow: rgba(0, 0, 0, 0.15) 0px 2px 8px 0px; color: hsl(206, 17%, 28%); font-size: 18px; margin: 0 auto; max-width: 800px; padding: 30px 40px; }.card-header::after { content: " "; display: block; width: 100%; background: #e7e9eb; height: 2px; }.card-body { min-height: 100px; }.card-footer { text-align: center; }.card-footer::before { content: " "; display: block; width: 100%; background: #e7e9eb; height: 2px; }.card-footer::after { content: " "; display: block; clear: both; }.btn { border: none; background-color: hsl(360, 91%, 36%); border-radius: 25px; box-shadow: rgba(0, 0, 0, 0.1) 0px 1px 6px 0px rgba(0, 0, 0, 0.2) 0px 1px 1px 0px; color: hsl(0, 0%, 100%); display: inline-block; font-size: 22px; line-height: 22px; margin: 16px 16px 16px 20px; padding: 14px 34px; text-align: center; cursor: pointer; } button[disabled] { cursor: default; background: #c0c7cf; }.float-right { float: right; } #password { -webkit-appearance: none; -moz-appearance: none; appearance: none; border: none; display: block; width: 100%; padding-top: 15px; padding-left: 15px; padding-right: 15px; padding-bottom: 85px; font-size: 1.2rem; text-align: center; margin-top: 10px; margin-bottom: 10px; border: 2px dashed #c0c7cf; border-radius: 6px; resize: none; overflow: hidden; } @media (max-width: 690px) {.btn { font-size: 1rem; margin: 16px 0px 0px 0px; padding: 10px 15px; } #password { font-size: 1rem; } } @media (max-width: 500px) {.btn { font-size: 0.8rem; } }
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width. initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Password Generator</title> <link rel="stylesheet" href="assets/css/style.css" /> </head> <body> <div class="wrapper"> <header> <h1>Password Generator</h1> </header> <div class="card"> <div class="card-header"> <h2>Generate a Password</h2> </div> <div class="card-body"> <textarea readonly id="password" placeholder="Your Secure Password" aria-label="Generated Password"></textarea> </div> <div class="card-footer"> <button id="generate" class="btn">Generate Password</button> </div> </div> </div> <script src="assets/js/script.js"></script> </body> </html>

You're never calling the selectCase() , selectNumeric() , or selectSpecial() functions. And the variables they define are local to those functions, so you can't access them from setPasswordCharacters() .

There's no reason to make these functions. Just use while loops to keep prompting until you get a proper answer.

// Function to set password characters
var setPasswordCharacters = function() {
  // when prompt answered input validated and character type selected
  let alphabet, numbers, special;
  while (alphabet === undefined) {
    var promptCase = window.prompt("Would you like your password to include UPPER case letters? Enter 'YES' or 'NO.'");
    switch (promptCase.toLowerCase()) {
      case "yes":
        alphabet = characters.lowercase + characters.uppercase;
        break;
      case "no":
        alphabet = characters.lowercase;
        break;
      default:
        window.alert("You need to provide a valid answer. Please try again.");
        break;
    }
  }
  while (numbers === undefined) {
    var promptNumeric = window.prompt("Would you like your password to include numbers? Enter 'YES' or 'NO.'");
    switch (promptNumeric.toLowerCase()) {
      case "yes":
        numbers = characters.numeric
        break;
      case "no":
        numbers = ""
        break;
      default:
        window.alert("You need to provide a valid answer. Please try again.");
        break;
    }
  }
  while (special === undefined) {
    var promptSpecial = window.prompt("Would you like your password to include special characters? Enter 'YES' or 'NO.'");
    switch (promptSpecial.toLowerCase()) {
      case "yes":
        special = characters.special
        break;
      case "no":
        special = ""
        break;
      default:
        window.alert("You need to provide a valid answer. Please try again.");
        break;
    }
  }
  // set password characters based on prompt responses
  password = alphabet + numbers + special;
  return;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM