为了账号安全,请及时绑定邮箱和手机立即绑定

使用随机密码生成器

使用随机密码生成器

斯蒂芬大帝 2023-06-29 21:07:28
我正在尝试使用以下代码生成随机密码。当我运行它时,我变得未定义。我的“未定义”从何而来?**```// 赋值代码   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);```
查看完整描述

3 回答

?
侃侃尔雅

TA贡献1801条经验 获得超15个赞

我修复了你的代码并在代码片段中做了一些观察


// Assignment Code


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 (~!@#$%^&*()-_=+)");

    }


    // userInput is an array, so you should push values into it.

    if (upper) {

        userInput.push(charString[0]);

    }


    if (lower) {

        userInput.push(charString[1])

    }


    if (number) {

        userInput.push(charString[2])

    }


    if (symbol) {

        userInput.push(charString[3])

    }


    var password = "";


    // join all array elements into a single one, and then split in order

    // to get an array of characters

    userInput = userInput.join("").split("");


    // You should start your for statement at position 0, not 1

    // and I guess it should be < than characterLength, not userInput.length

    for (var i = 0; i < characterLength; i++) {

        // Math.random is a function so you were missing the parenthesis Math.random()

        // also Math.random() should be times userInput.length

        // so they should be inside the same parenthesis

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

        password = password + userInput[index]

    }


    return password

}


// Add event listener to generate button

generateBtn.addEventListener("click", writePassword);

<button id="generate">Generate</button>

<br>

<input type="text" id="password">


查看完整回答
反对 回复 2023-06-29
?
HUH函数

TA贡献1836条经验 获得超4个赞

我对您的类型选择进行了一些更改以避免无限循环。你的问题是你的随机索引生成。就这样:


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

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


var userInput = "";


var password = "";


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


// Write password to the #password input

function writePassword() {

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

  password = generatePassword();

  passwordText.value = ''; // Clear prev value

  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 (true) {

    // 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 (~!@#$%^&*()-_=+)");


    break;

  }


  console.log('Using upper', upper);

  console.log('Using lower', lower);

  console.log('Using number', number);

  console.log('Using symbol', symbol);


  upper && (userInput += charString[0])


  lower && (userInput += charString[1])


  number && (userInput += charString[2])


  symbol && (userInput += charString[3])


  var password = "";


  for (var i = 0; i < characterLength; i++) {

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

    password += userInput[index]

  }


  return password


}




// Add event listener to generate button

generateBtn.addEventListener("click", writePassword);

<button id="generate">Generate</button>

<input id="password"/>


查看完整回答
反对 回复 2023-06-29
?
手掌心

TA贡献1942条经验 获得超3个赞

改变

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

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


查看完整回答
反对 回复 2023-06-29
  • 3 回答
  • 0 关注
  • 162 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信