簡體   English   中英

為什么我的密碼生成代碼沒有運行?

[英]Why is my password generation code not running?

我寫了這段代碼,應該生成一個隨機密碼。 以下是要求。

  1. 密碼介於 1-128 個字符之間
  2. 密碼包含大寫、小寫、數字和特殊字符,具體取決於用戶對詢問他們是否希望密碼包含這些字符的提示的響應。
  3. 當用戶單擊“生成密碼”按鈕時,它應該生成隨機密碼(基於提示響應)並在更改和文本區域框中顯示密碼

我不知道為什么我的 generatePassword() function 代碼沒有運行。 以下是 Chrome 開發人員工具顯示的錯誤以及 javascript 代碼。

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>

您永遠不會調用selectCase()selectNumeric()selectSpecial()函數。 他們定義的變量是這些函數的本地變量,因此您無法從setPasswordCharacters()訪問它們。

沒有理由制作這些功能。 只需使用while循環來不斷提示,直到您得到正確的答案。

// 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;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM