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

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

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

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

function writePassword() {
  var password = generatePassword();
  var passwordText = document.querySelector('#password');

  passwordText.value = password;
}

function generatePassword() {
  userpassword = "";
  passwordCharacters = "";

  let passwordlength = prompt("Select your desired password length");

  if (passwordlength < 8 || passwordlength > 128 || passwordlength == isNaN) {
  alert("Password length must be between 8 and 128 characters and numerical.");
  return null;
  }
    var includespecial = confirm('Would you like to include special characters?');
    var includeupper = confirm('Would you like to include uppercase characters?');
    var includelower = confirm('Would you like to include lowercase characters?');
    var includenumbers= confirm('Would you like to include numbers?');

    var uppercasecharcodes = arrayFromLowToHigh(65, 90);
    var lowercasecharcodes = arrayFromLowToHigh(97, 122);
    var numbercharcodes = arrayFromLowToHigh(48, 57);
    var specialcharcodes = arrayFromLowToHigh(33, 47).concat(
      arrayFromLowToHigh(58, 64)
      ).concat(
        arrayFromLowToHigh(91, 96)
      ).concat(
        arrayFromLowToHigh(123, 126)
      );

    if (!includelower && !includenumbers && !includeupper && !includespecial) {
    alert('You must choose atleast one of the options!');
    return null;
  }
    // MERGE section where all options are compiled
  if (includespecial) {
    passwordCharacters += specialcharcodes;
  }if (includeupper) {
    passwordCharacters += uppercasecharcodes;
  }if (includelower) {
    passwordCharacters += lowercasecharcodes;
  }if (includenumbers) {
    passwordCharacters += numbercharcodes;
  }

  for (var i = 0; i < passwordlength; i++) {
      userpassword += passwordCharacters[Math.floor(Math.random() * passwordlength)];
      userpassword.push(String.fromCharCode(passwordCharacters));

  }
  return userpassword;
}
generateBtn.addEventListener("click",writePassword);

// FUNCTION for above arrayFromLowToHigh
function arrayFromLowToHigh(low, high) {
  const array = []
  for (let i = low; i <= high; i++) {
    array.push(i)
  }
  return array
}
eeq64g8w

eeq64g8w1#

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

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

    function writePassword() {
        var password = generatePassword();
        document.getElementById('password').innerHTML = "";
        document.getElementById('password').innerHTML = password;
        console.log('pass was created, with a length of ' + document.getElementById('password').innerHTML.length)
    }

    function generatePassword() {
        var userpassword = "";
        var passwordCharacters = "";
        var isvalid = true;
        let passwordlength = prompt("Select your desired password length");
        if(passwordlength < 8 || passwordlength > 128 || isNaN(passwordlength)) {
            alert("Password length must be between 8 and 128 characters and numerical.");
            var isvalid = false;
            return;
        }
        if(isvalid) {
            var includespecial = confirm('Would you like to include special characters?');
            var includeupper = confirm('Would you like to include uppercase characters?');
            var includelower = confirm('Would you like to include lowercase characters?');
            var includenumbers = confirm('Would you like to include numbers?');
            var uppercasecharcodes = arrayFromLowToHigh(65, 90);
            var lowercasecharcodes = arrayFromLowToHigh(97, 122);
            var numbercharcodes = arrayFromLowToHigh(48, 57);
            var specialcharcodes = arrayFromLowToHigh(33, 47).concat(arrayFromLowToHigh(58, 64)).concat(arrayFromLowToHigh(91, 96)).concat(arrayFromLowToHigh(123, 126));
            if(!includelower && !includenumbers && !includeupper && !includespecial) {
                alert('You must choose atleast one of the options!');
                return null;
            }
            // MERGE section where all options are compiled
            if(includespecial) {
                passwordCharacters += specialcharcodes;
            }
            if(includeupper) {
                passwordCharacters += uppercasecharcodes;
            }
            if(includelower) {
                passwordCharacters += lowercasecharcodes;
            }
            if(includenumbers) {
                passwordCharacters += numbercharcodes;
            }
            var userpasswordto = [];
            for(var i = 0; i <= passwordlength; i++) {
                var userpasswordtoadd = Math.floor(Math.random() * passwordCharacters.length);
                var userpasswordstring = String.fromCharCode(userpasswordtoadd);
                var userpasswordstring = userpasswordstring.replace(/\s/g, '-'); //replace spacewhites, is a probabily to be apear
                userpasswordto.push(userpasswordstring);
            }
            var makestring = userpasswordto.length;
            for(var i = 0; i < makestring; i++) {
                var userpassword = userpassword + userpasswordto[i];
            }
            return userpassword;
        }
    }
    generateBtn.addEventListener("click", writePassword);
    // FUNCTION for above arrayFromLowToHigh
    function arrayFromLowToHigh(low, high) {
        const array = []
        for(let i = low; i <= high; i++) {
            array.push(i)
        }
        return array
    }
body {
        font-family: helvetica, arial, sans-serif;
        font-weight: 300;
    }

    .table {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background-color: #232323;
        overflow: hidden;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100vh;
        width: 100vw;
    }

    .table .key {
        background-color: rgba(0, 0, 0, 0.38);
        color: rgba(255, 255, 255, 0.4);
        border-radius: 0.22vw;
        user-select: none;
        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);
        position: relative;
        cursor: default;
        padding: 16px;
        font-size: 16px;
margin-top:-100px;
    }

    .table .key:hover {
        box-shadow: 0 0 0.3rem 0 rgba(0, 0, 0, 0.7), 0 0 0.2rem 0rem black;
        transform: scale(0.99);
    }

    .table .passs {
        width: 700px;
        background-color: rgba(0, 0, 0, 0.38);
        color: rgba(255, 255, 255, 0.4);
        border-radius: 0.22vw;
        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);
        position: relative;
        cursor: default;
        padding: 16px;
        font-size: 16px;
        margin-left: 50px;
        background: rgba(0, 0, 0, 0.38);
        color: rgba(255, 255, 255, 0.4);
    }

    .pass {
        width: 100%;
        float: none;
        position: absolute;
        text-align: center;
        margin-top: 50px;
        color: rgb(74, 234, 1);
        ;
    }

    pre.code code {
        font-family: "Inconsolata", "Monaco", "Consolas", "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace;
        display: block;
        margin: 0 0 0 60px;
        padding: 15px 16px 14px;
        border-left: 1px solid #555;
        overflow-x: auto;
        font-size: 13px;
        line-height: 19px;
        color: #ddd;
    }

    pre {
        background: #333;
        white-space: pre;
        word-wrap: break-word;
        overflow: auto;
        padding: 15px 0;
    }
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Terrible generador de pass</title>

</head>

<body>
    <div class="table">
        <div class="key" id="generate">GENERAR PASS</div>
        <div class="pass">
            <pre><code id="password"></code></pre>
        </div>
    </div>

</body>

</html>
nfeuvbwi

nfeuvbwi2#

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

相关问题