很难从ascii码数组中获取字符串

yc0p9oo0  于 2021-09-23  发布在  Java
关注(0)|答案(2)|浏览(416)

我正在做一个密码生成器项目,为我的字符选项实现ascii码,我很难从数组中获取字符串。任何帮助都将不胜感激。我已经运行了几个测试,似乎我的问题是for循环和从我拥有的内容中获取字符串。对不起,如果这是一个愚蠢的问题。对这一点仍然非常陌生,尤其是javascript。非常感谢!!

  1. var generateBtn = document.querySelector("#generate");
  2. function writePassword() {
  3. var password = generatePassword();
  4. var passwordText = document.querySelector('#password');
  5. passwordText.value = password;
  6. }
  7. function generatePassword() {
  8. userpassword = "";
  9. passwordCharacters = "";
  10. let passwordlength = prompt("Select your desired password length");
  11. if (passwordlength < 8 || passwordlength > 128 || passwordlength == isNaN) {
  12. alert("Password length must be between 8 and 128 characters and numerical.");
  13. return null;
  14. }
  15. var includespecial = confirm('Would you like to include special characters?');
  16. var includeupper = confirm('Would you like to include uppercase characters?');
  17. var includelower = confirm('Would you like to include lowercase characters?');
  18. var includenumbers= confirm('Would you like to include numbers?');
  19. var uppercasecharcodes = arrayFromLowToHigh(65, 90);
  20. var lowercasecharcodes = arrayFromLowToHigh(97, 122);
  21. var numbercharcodes = arrayFromLowToHigh(48, 57);
  22. var specialcharcodes = arrayFromLowToHigh(33, 47).concat(
  23. arrayFromLowToHigh(58, 64)
  24. ).concat(
  25. arrayFromLowToHigh(91, 96)
  26. ).concat(
  27. arrayFromLowToHigh(123, 126)
  28. );
  29. if (!includelower && !includenumbers && !includeupper && !includespecial) {
  30. alert('You must choose atleast one of the options!');
  31. return null;
  32. }
  33. // MERGE section where all options are compiled
  34. if (includespecial) {
  35. passwordCharacters += specialcharcodes;
  36. }if (includeupper) {
  37. passwordCharacters += uppercasecharcodes;
  38. }if (includelower) {
  39. passwordCharacters += lowercasecharcodes;
  40. }if (includenumbers) {
  41. passwordCharacters += numbercharcodes;
  42. }
  43. for (var i = 0; i < passwordlength; i++) {
  44. userpassword += passwordCharacters[Math.floor(Math.random() * passwordlength)];
  45. userpassword.push(String.fromCharCode(passwordCharacters));
  46. }
  47. return userpassword;
  48. }
  49. generateBtn.addEventListener("click",writePassword);
  50. // FUNCTION for above arrayFromLowToHigh
  51. function arrayFromLowToHigh(low, high) {
  52. const array = []
  53. for (let i = low; i <= high; i++) {
  54. array.push(i)
  55. }
  56. return array
  57. }
eeq64g8w

eeq64g8w1#

我修正了你代码的一些部分,这是最后的代码,探索它,告诉我发生了什么,谢谢!

  1. var generateBtn = document.querySelector("#generate");
  2. function writePassword() {
  3. var password = generatePassword();
  4. document.getElementById('password').innerHTML = "";
  5. document.getElementById('password').innerHTML = password;
  6. console.log('pass was created, with a length of ' + document.getElementById('password').innerHTML.length)
  7. }
  8. function generatePassword() {
  9. var userpassword = "";
  10. var passwordCharacters = "";
  11. var isvalid = true;
  12. let passwordlength = prompt("Select your desired password length");
  13. if(passwordlength < 8 || passwordlength > 128 || isNaN(passwordlength)) {
  14. alert("Password length must be between 8 and 128 characters and numerical.");
  15. var isvalid = false;
  16. return;
  17. }
  18. if(isvalid) {
  19. var includespecial = confirm('Would you like to include special characters?');
  20. var includeupper = confirm('Would you like to include uppercase characters?');
  21. var includelower = confirm('Would you like to include lowercase characters?');
  22. var includenumbers = confirm('Would you like to include numbers?');
  23. var uppercasecharcodes = arrayFromLowToHigh(65, 90);
  24. var lowercasecharcodes = arrayFromLowToHigh(97, 122);
  25. var numbercharcodes = arrayFromLowToHigh(48, 57);
  26. var specialcharcodes = arrayFromLowToHigh(33, 47).concat(arrayFromLowToHigh(58, 64)).concat(arrayFromLowToHigh(91, 96)).concat(arrayFromLowToHigh(123, 126));
  27. if(!includelower && !includenumbers && !includeupper && !includespecial) {
  28. alert('You must choose atleast one of the options!');
  29. return null;
  30. }
  31. // MERGE section where all options are compiled
  32. if(includespecial) {
  33. passwordCharacters += specialcharcodes;
  34. }
  35. if(includeupper) {
  36. passwordCharacters += uppercasecharcodes;
  37. }
  38. if(includelower) {
  39. passwordCharacters += lowercasecharcodes;
  40. }
  41. if(includenumbers) {
  42. passwordCharacters += numbercharcodes;
  43. }
  44. var userpasswordto = [];
  45. for(var i = 0; i <= passwordlength; i++) {
  46. var userpasswordtoadd = Math.floor(Math.random() * passwordCharacters.length);
  47. var userpasswordstring = String.fromCharCode(userpasswordtoadd);
  48. var userpasswordstring = userpasswordstring.replace(/\s/g, '-'); //replace spacewhites, is a probabily to be apear
  49. userpasswordto.push(userpasswordstring);
  50. }
  51. var makestring = userpasswordto.length;
  52. for(var i = 0; i < makestring; i++) {
  53. var userpassword = userpassword + userpasswordto[i];
  54. }
  55. return userpassword;
  56. }
  57. }
  58. generateBtn.addEventListener("click", writePassword);
  59. // FUNCTION for above arrayFromLowToHigh
  60. function arrayFromLowToHigh(low, high) {
  61. const array = []
  62. for(let i = low; i <= high; i++) {
  63. array.push(i)
  64. }
  65. return array
  66. }
  1. body {
  2. font-family: helvetica, arial, sans-serif;
  3. font-weight: 300;
  4. }
  5. .table {
  6. position: absolute;
  7. top: 0;
  8. right: 0;
  9. bottom: 0;
  10. left: 0;
  11. background-color: #232323;
  12. overflow: hidden;
  13. display: flex;
  14. align-items: center;
  15. justify-content: center;
  16. height: 100vh;
  17. width: 100vw;
  18. }
  19. .table .key {
  20. background-color: rgba(0, 0, 0, 0.38);
  21. color: rgba(255, 255, 255, 0.4);
  22. border-radius: 0.22vw;
  23. user-select: none;
  24. box-shadow: 0.2rem 0.1rem 0.5rem 0 rgba(0, 0, 0, 0.5), 0 0 0.1rem 0.1rem rgba(0, 0, 0, 0.5);
  25. position: relative;
  26. cursor: default;
  27. padding: 16px;
  28. font-size: 16px;
  29. margin-top:-100px;
  30. }
  31. .table .key:hover {
  32. box-shadow: 0 0 0.3rem 0 rgba(0, 0, 0, 0.7), 0 0 0.2rem 0rem black;
  33. transform: scale(0.99);
  34. }
  35. .table .passs {
  36. width: 700px;
  37. background-color: rgba(0, 0, 0, 0.38);
  38. color: rgba(255, 255, 255, 0.4);
  39. border-radius: 0.22vw;
  40. box-shadow: 0.2rem 0.1rem 0.5rem 0 rgba(0, 0, 0, 0.5), 0 0 0.1rem 0.1rem rgba(0, 0, 0, 0.5);
  41. position: relative;
  42. cursor: default;
  43. padding: 16px;
  44. font-size: 16px;
  45. margin-left: 50px;
  46. background: rgba(0, 0, 0, 0.38);
  47. color: rgba(255, 255, 255, 0.4);
  48. }
  49. .pass {
  50. width: 100%;
  51. float: none;
  52. position: absolute;
  53. text-align: center;
  54. margin-top: 50px;
  55. color: rgb(74, 234, 1);
  56. ;
  57. }
  58. pre.code code {
  59. font-family: "Inconsolata", "Monaco", "Consolas", "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace;
  60. display: block;
  61. margin: 0 0 0 60px;
  62. padding: 15px 16px 14px;
  63. border-left: 1px solid #555;
  64. overflow-x: auto;
  65. font-size: 13px;
  66. line-height: 19px;
  67. color: #ddd;
  68. }
  69. pre {
  70. background: #333;
  71. white-space: pre;
  72. word-wrap: break-word;
  73. overflow: auto;
  74. padding: 15px 0;
  75. }
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Terrible generador de pass</title>
  6. </head>
  7. <body>
  8. <div class="table">
  9. <div class="key" id="generate">GENERAR PASS</div>
  10. <div class="pass">
  11. <pre><code id="password"></code></pre>
  12. </div>
  13. </div>
  14. </body>
  15. </html>
展开查看全部
nfeuvbwi

nfeuvbwi2#

去除 userpassword.push(String.fromCharCode(passwordCharacters)); 因为userpassword不是数组,所以woule循环没有什么意义。

相关问题