【问题标题】:Did I write/use concat correctly for a password generator in Javascript?我是否为 Javascript 中的密码生成器正确编写/使用 concat?
【发布时间】:2025-12-05 23:25:01
【问题描述】:

我们必须生成一个 8-128 个字符之间的随机密码。用户应该为他们想要包含的字符(大写、小写、数字和特殊字符)获得一系列确定/取消选项。然后应该生成密码。我不确定我是否正确使用了 concat。尽管弹出确认窗口,但该按钮不会生成任何内容。不确定我是否只是贴错了标签,或者我完全不在了。

//Lowercase
 var lowerCase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];

 //Uppercase
 var upperCase = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", ];

 //Numeric
 var numValue = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

 //Special Characters
 var spChar = ['@', '%', '+', '\\', '/', "'", '!', '#', '$', '^', '?', ':', ',', ')', '(', '}', '{', ']', '[', '~', '-', '_', '.'];

 //var choices outside the if statement to concat Not sure if I should put this inside a function

 var choices;

 // Length of password, this is where the user selects how long their password should be(8 - 128 characters)

 function generatePassword() {

     var number = 0
     while (number < 8 || number > 128) {
         number = window.prompt("How long would you like your password to be? Enter a number between 8-128");
         console.log(number);
     }

     //Where while loop ends
     console.log("after ", number)

     //Prompt for character types
     var lcChoice = confirm("Would you like your password to contain lowercase characters?");
     var ucChoice = confirm("How about uppercase characters?");
     var numchoice = confirm("Would you like numbers?");
     var spChoice = confirm("How about special characters? Like & or $?");

     //If for  negative on all 4 options - this doesn't return anything

     if (!lcChoice && !ucChoice && !numchoice && spChoice) {
         prompt = alert("You must choose a criteria!");
     }

     //Else if for all 4 character options
     if (lcChoice && ucChoice && numchoice && spChoice) {
         choices = spChar.concat(numValue, upperCase, lowerCase);
     }

     //Else if for 3 options (lc, uc, n) (uc, n, sp) (n, sp, lc) (sp, lc, uc)
     else if (lcChoice && ucChoice && numchoice) {
         choices = lcCase.concat(numValue, upperCase);
     } else if (lcChoice && ucChoice && spChoice) {
         choices = lowerCase.concat(spChar, upperCase);
     } else if (lcChoice && spChoice && numchoice) {
         choices = lowerCase.concat(numValue, spChar);
     } else if (spChoice && ucChoice && numchoice) {
         choices = spChar.concat(numValue, upperCase);
     }

     //Else if for 2 options (sp,n) (sp,lc) (sp, uc) (lc, n) (lc, uc) (n, uc)
     else if (spChoice && numchoice) {
         choices = spChar.concat(numValue);
     } else if (spChoice && lcChoice) {
         choices = spChar.concat(lowerCase);
     } else if (spChoice && ucChoice) {
         choices = spChar.concat(upperCase);
     } else if (lcChoice && numchoice) {
         choices = lowerCase.concat(numValue);
     } else if (lcChoice && ucChoice) {
         choices = lowerCase.concat(upperCase);
     } else if (numchoice && ucChoice) {
         choices = numValue.concat(upperCase);
     }

     //else if for one choice
     else if (lcChoice) {
         choices = lowerCase;
     } else if (ucChoice) {
         choices = upperCase;
     } else if (numchoice) {
         choices = numValue;
     } else if (spChoice) {
         choice = spChar;
     }

     var pwd = [];
     for (i = 0; i === number; i++) {
         pwd += choices[Math.floor(Math.random() * choices.length)]
     }

     
     return pwd;
 }

 //Get references to the #generate element
 var generateBtn = document.querySelector("#generate");

 //Write password to the #password input
 function writePassword() {
     var password = generatePassword();
     var passwordText = document.querySelector("#password");

     passwordText.value = password;
 }

 //Add event listener to generate button - This code was given to us
 generateBtn.addEventListener("click", writePassword);

【问题讨论】:

  • 很难判断您的代码中是否确实存在错误,或者您是否认为代码块中间会出现粗体文本。请修复您的代码....
  • choices = alert("You must choose a criteria!");
  • 您可以将代码更改为 4 个 if 语句,没有理由进行所有组合。

标签: javascript arrays concatenation


【解决方案1】:

我认为您没有从密码生成器中获得任何信息的原因在这里:

for (i = 0; i === number; i++) {
        pwd += choices[Math.floor(Math.random() * choices.number)]
    }

choices.number 未定义(这里指的是choices对象的number属性,不存在)

请尝试将其更改为choices.length(数组的长度)

【讨论】:

    【解决方案2】:

    您的代码中有几个错误,其中包括 choices = spChoice.concat(numValue, upperCase, lowerCase); 应该是 spChar 和类似的 choices = lcChoice.concat(numValue, upperCase);。然后这里pwd += choices[Math.floor(Math.random() * choices.number)] 没有choices.number,因为它是一个字符串。它应该是choices.length

    //Lowercase
     var lowerCase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
    
     //Uppercase
     var upperCase = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", ];
    
     //Numeric
     var numValue = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    
     //Special Characters
     var spChar = ['@', '%', '+', '\\', '/', "'", '!', '#', '$', '^', '?', ':', ',', ')', '(', '}', '{', ']', '[', '~', '-', '_', '.'];
    
     //var choices outside the if statement to concat Not sure if I should put this inside a function
    
     var choices;
    
     // Length of password, this is where the user selects how long their password should be(8 - 128 characters)
    
     function generatePassword() {
    
         var number = 0
         while (number < 8 || number > 128) {
             number = window.prompt("How long would you like your password to be? Enter a number between 8-128");
             console.log(number);
         }
    
         //Where while loop ends
         console.log("after ", number)
    
         //Prompt for character types
         var lcChoice = confirm("Would you like your password to contain lowercase characters?");
         var ucChoice = confirm("How about uppercase characters?");
         var numchoice = confirm("Would you like numbers?");
         var spChoice = confirm("How about special characters? Like & or $?");
    
         //If for  negative on all 4 options - this doesn't return anything
    
         if (!lcChoice && !ucChoice && !numchoice && !spChoice) {
             prompt = alert("You must choose a criteria!");
         }
    
         //Else if for all 4 character options
         if (lcChoice && ucChoice && numchoice && spChoice) {
             choices = spChar.concat(numValue, upperCase, lowerCase);
         }
    
         //Else if for 3 options (lc, uc, n) (uc, n, sp) (n, sp, lc) (sp, lc, uc)
         else if (lcChoice && ucChoice && numchoice) {
             choices = lowerCase.concat(numValue, upperCase);
         } else if (lcChoice && ucChoice && spChoice) {
             choices = lowerCase.concat(spChar, upperCase);
         } else if (lcChoice && spChoice && numchoice) {
             choices = lowerCase.concat(numValue, spChar);
         } else if (spChoice && ucChoice && numchoice) {
             choices = spChar.concat(numValue, upperCase);
         }
    
         //Else if for 2 options (sp,n) (sp,lc) (sp, uc) (lc, n) (lc, uc) (n, uc)
         else if (spChoice && numchoice) {
             choices = spChar.concat(numValue);
         } else if (spChoice && lcChoice) {
             choices = spChar.concat(lowerCase);
         } else if (spChoice && ucChoice) {
             choices = spChar.concat(upperCase);
         } else if (lcChoice && numchoice) {
             choices = lowerCase.concat(numValue);
         } else if (lcChoice && ucChoice) {
             choices = lowerCase.concat(upperCase);
         } else if (numchoice && ucChoice) {
             choices = numValue.concat(upperCase);
         }
    
         //else if for one choice
         else if (lcChoice) {
             choices = lowerCase;
         } else if (ucChoice) {
             choices = upperCase;
         } else if (numchoice) {
             choices = numValue;
         } else if (spChoice) {
             choice = spChar;
         }
    
         var pwd = [];
         for (i = 0; i < number; i++) {
             pwd += choices[Math.floor(Math.random() * choices.length)]
         }
    
         return pwd;
     }
    
     //Get references to the #generate element
     var generateBtn = document.querySelector("#generate");
    
     //Write password to the #password input
     function writePassword() {
         var password = generatePassword();
         var passwordText = document.querySelector("#password");
          console.log(password)
         passwordText.value = password;
     }
    
     //Add event listener to generate button - This code was given to us
     generateBtn.addEventListener("click", writePassword);
    <input type="text" id="password">
    <button id="generate">Generate</button>

    【讨论】:

    • 感谢您的收看!再次检查后,它仍然没有生成代码?我的最后一个 for 循环不正确吗?
    • 您实际上可以在 SO 上运行我的代码并查看。我已经为您解决了我在答案中提到的问题。
    【解决方案3】:

    编辑标签后,我需要将函数的长度或迭代更改为 i

    【讨论】:

      最近更新 更多