使用随机密码生成器

我正在尝试使用以下代码生成随机密码。当我运行它时,我变得未定义。


我的“未定义”从何而来?**


```

// 赋值代码


   var charString = ["ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklnmopqrstuvwxyz", "0123456789", 

 "~!@#$%^&*()-_=+"];



 var userInput = [];


 var password = "";


 var generateBtn = document.querySelector("#generate");


    // Write password to the #password input

  function writePassword() {

 password = generatePassword();

   var passwordText = document.querySelector("#password");


  passwordText.value = password;


 }


 // Character length prompt

 function generatePassword() {

  var characterLength = 0

   while ((characterLength < 8 || characterLength > 128) || Number.isInteger(characterLength) === 

false) {

 characterLength = parseInt(prompt("How many characters would you like your password to be? (8- 

128)"));


   }


   var upper = false

   var lower = false

   var number = false

    var symbol = false


  while (!upper && !lower && !number && !symbol) {


  // Uppercase pop up

    upper = confirm("Click OK to confirm uppercase characters");


   // Lowercase pop up

   lower = confirm("Click OK to confirm lowercase characters");


   // Numeric pop up

   number = confirm("Click OK to confirm numeric characters");


   // Special charcter  pop up

  symbol = confirm("Click OK to confirm special characters (~!@#$%^&*()-_=+)");


  }


   if (upper) {

      userInput += charString[0];


   }


   if (lower) {

      userInput += charString[1]


   }



    if (number) {

     userInput += charString[2]


     }



    if (symbol) {

     userInput += charString[3]


     }


     var password = "";


   for (var i = 1; i <= userInput.length; i++) {

      var index = (Math.floor(Math.random) * userInput);

     password = password + userInput[index]

  }


  return password


}




// Add event listener to generate button

 generateBtn.addEventListener("click", writePassword);



```


斯蒂芬大帝
浏览 163回答 3
3回答

侃侃尔雅

我修复了你的代码并在代码片段中做了一些观察// Assignment Codevar charString = ["ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklnmopqrstuvwxyz", "0123456789", "~!@#$%^&*()-_=+"];var userInput = [];var password = "";var generateBtn = document.querySelector("#generate");// Write password to the #password inputfunction writePassword() {&nbsp; &nbsp; password = generatePassword();&nbsp; &nbsp; var passwordText = document.querySelector("#password");&nbsp; &nbsp; passwordText.value = password;}// Character length promptfunction generatePassword() {&nbsp; &nbsp; var characterLength = 0&nbsp; &nbsp; while ((characterLength < 8 || characterLength > 128) || Number.isInteger(characterLength) === false) {&nbsp; &nbsp; &nbsp; &nbsp; characterLength = parseInt(prompt("How many characters would you like your password to be? (8-128)"));&nbsp; &nbsp; }&nbsp; &nbsp; var upper = false&nbsp; &nbsp; var lower = false&nbsp; &nbsp; var number = false&nbsp; &nbsp; var symbol = false&nbsp; &nbsp; while (!upper && !lower && !number && !symbol) {&nbsp; &nbsp; &nbsp; &nbsp; // Uppercase pop up&nbsp; &nbsp; &nbsp; &nbsp; upper = confirm("Click OK to confirm uppercase characters");&nbsp; &nbsp; &nbsp; &nbsp; // Lowercase pop up&nbsp; &nbsp; &nbsp; &nbsp; lower = confirm("Click OK to confirm lowercase characters");&nbsp; &nbsp; &nbsp; &nbsp; // Numeric pop up&nbsp; &nbsp; &nbsp; &nbsp; number = confirm("Click OK to confirm numeric characters");&nbsp; &nbsp; &nbsp; &nbsp; // Special charcter&nbsp; pop up&nbsp; &nbsp; &nbsp; &nbsp; symbol = confirm("Click OK to confirm special characters (~!@#$%^&*()-_=+)");&nbsp; &nbsp; }&nbsp; &nbsp; // userInput is an array, so you should push values into it.&nbsp; &nbsp; if (upper) {&nbsp; &nbsp; &nbsp; &nbsp; userInput.push(charString[0]);&nbsp; &nbsp; }&nbsp; &nbsp; if (lower) {&nbsp; &nbsp; &nbsp; &nbsp; userInput.push(charString[1])&nbsp; &nbsp; }&nbsp; &nbsp; if (number) {&nbsp; &nbsp; &nbsp; &nbsp; userInput.push(charString[2])&nbsp; &nbsp; }&nbsp; &nbsp; if (symbol) {&nbsp; &nbsp; &nbsp; &nbsp; userInput.push(charString[3])&nbsp; &nbsp; }&nbsp; &nbsp; var password = "";&nbsp; &nbsp; // join all array elements into a single one, and then split in order&nbsp; &nbsp; // to get an array of characters&nbsp; &nbsp; userInput = userInput.join("").split("");&nbsp; &nbsp; // You should start your for statement at position 0, not 1&nbsp; &nbsp; // and I guess it should be < than characterLength, not userInput.length&nbsp; &nbsp; for (var i = 0; i < characterLength; i++) {&nbsp; &nbsp; &nbsp; &nbsp; // Math.random is a function so you were missing the parenthesis Math.random()&nbsp; &nbsp; &nbsp; &nbsp; // also Math.random() should be times userInput.length&nbsp; &nbsp; &nbsp; &nbsp; // so they should be inside the same parenthesis&nbsp; &nbsp; &nbsp; &nbsp; var index = (Math.floor(Math.random() * userInput.length));&nbsp; &nbsp; &nbsp; &nbsp; password = password + userInput[index]&nbsp; &nbsp; }&nbsp; &nbsp; return password}// Add event listener to generate buttongenerateBtn.addEventListener("click", writePassword);<button id="generate">Generate</button><br><input type="text" id="password">

HUH函数

我对您的类型选择进行了一些更改以避免无限循环。你的问题是你的随机索引生成。就这样:var charString = ["ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklnmopqrstuvwxyz", "0123456789",&nbsp;&nbsp;"~!@#$%^&*()-_=+"];var userInput = "";var password = "";var generateBtn = document.querySelector("#generate");// Write password to the #password inputfunction writePassword() {&nbsp; var passwordText = document.querySelector("#password");&nbsp; password = generatePassword();&nbsp; passwordText.value = ''; // Clear prev value&nbsp; passwordText.value = password;}// Character length promptfunction generatePassword() {&nbsp; var characterLength = 0&nbsp; while ((characterLength < 8 || characterLength > 128) || Number.isInteger(characterLength) ===&nbsp;false) {&nbsp; characterLength = parseInt(prompt(`How many characters would you like your password to be? (8&nbsp;128)`));&nbsp; }&nbsp; var upper = false&nbsp; var lower = false&nbsp; var number = false&nbsp; var symbol = false&nbsp; while (true) {&nbsp; &nbsp; // Uppercase pop up&nbsp; &nbsp; upper = confirm("Click OK to confirm uppercase characters");&nbsp; &nbsp; // Lowercase pop up&nbsp; &nbsp; lower = confirm("Click OK to confirm lowercase characters");&nbsp; &nbsp; // Numeric pop up&nbsp; &nbsp; number = confirm("Click OK to confirm numeric characters");&nbsp; &nbsp; // Special charcter&nbsp; pop up&nbsp; &nbsp; symbol = confirm("Click OK to confirm special characters (~!@#$%^&*()-_=+)");&nbsp; &nbsp; break;&nbsp; }&nbsp; console.log('Using upper', upper);&nbsp; console.log('Using lower', lower);&nbsp; console.log('Using number', number);&nbsp; console.log('Using symbol', symbol);&nbsp; upper && (userInput += charString[0])&nbsp; lower && (userInput += charString[1])&nbsp; number && (userInput += charString[2])&nbsp; symbol && (userInput += charString[3])&nbsp; var password = "";&nbsp; for (var i = 0; i < characterLength; i++) {&nbsp; &nbsp; var index = Math.floor(Math.random() * userInput.length);&nbsp; &nbsp; password += userInput[index]&nbsp; }&nbsp; return password}// Add event listener to generate buttongenerateBtn.addEventListener("click", writePassword);<button id="generate">Generate</button><input id="password"/>

手掌心

改变&nbsp;var&nbsp;index&nbsp;=&nbsp;(Math.floor(Math.random)&nbsp;*&nbsp;userInput);到&nbsp;var&nbsp;index&nbsp;=&nbsp;(Math.floor(Math.random()&nbsp;*&nbsp;userInput.length));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript