JavaScript 密码生成器数值问题

JavaScript Password Generator Numerical Problem

我刚刚开始了一个编码训练营,对于我们的第三个任务,我们必须编写一个具有一些用户首选项的随机密码生成器。我写了我的代码,除了一位之外,一切都正常工作。当用户只选择数值时,会出现错误,我的字符选择池告诉我 "pSelection.charAt is not a function at generatePWD".

考虑到该功能在所有其他情况下都能完美运行,这对我来说毫无意义。我已经尝试为失败的特定(else if)语句创建一个单独的数组。我试过交换变量名以防 "Char".

发生冲突

如果有人可以看看并告诉我我做错了什么,我会很感激你的帮助,因为我以为我明白发生了什么,但显然不是..

这是 JavaScript - 下面是 HTML

// Add event listener to the generate button
document.getElementById("generate").addEventListener("click", function(){

// Setting up the possible Characters
  var lowercaseAlphabetChar = ("abcdefghijklmnopqrstuvwxyz");
  var uppercaseAlphabetChar = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  var numericChar = (1234567890);
  var specialChar = ("!@#$%^&*()?~/|:'><");
  var password = "";
  var pSelection = "";

// User Prompts
  var length = prompt("How long is your Password? Choose between 8 and 128 Characters");

// Making sure User Chooses between 8 and 128 Characters
  if (length < 8 || length > 128){
    alert("Length has to be Between 8 and 128. Please Try Again")
    return
  }

  var wantsLower = confirm("Do you want Lowercase Characters?")
  var wantsUpper = confirm("Do you want Uppercase Characters?")
  var wantsNumbers = confirm("Do you want Numerical Characters?")
  var wantsSpecial = confirm("Do you want Special Characters?")

//Verification Points System - I allocate a point when an option is confirmed. if no points are allocated, user has not selected anything
  var verify = 0;

  if (wantsLower == true || wantsUpper == true || wantsNumbers == true || wantsSpecial == true) {
    verify += 1;
  }

  if (verify == 0) {
    alert("You have not selected a character Type, Please Try again.")
    return
  }

// Script will exit if not verified, if verified we Proceed to Grouping Password to User Needs.
  function generatePWD() {
    for (var i = 0; i <= length; i++) {
      password = password + pSelection.charAt(Math.floor(Math.random() * Math.floor(pSelection.length - 1)));
    }
  }

// IF STATEMENTS for User PreferenceLowerCase
  if (wantsLower && !wantsUpper && !wantsNumbers && !wantsSpecial) {
    pSelection = lowercaseAlphabetChar;
    generatePWD();
  }
// LowerCase and UpperCase
  else if (wantsLower && wantsUpper && !wantsNumbers && !wantsSpecial) {
    pSelection = lowercaseAlphabetChar + uppercaseAlphabetChar;
    generatePWD();
  }

//LowerCase and UpperCase and Numbers
  else if (wantsLower && wantsUpper && wantsNumbers && !wantsSpecial) {
    pSelection = lowercaseAlphabetChar + uppercaseAlphabetChar + numericChar;
    generatePWD();
  }
// Everything
  else if (wantsLower && wantsUpper && wantsNumbers && wantsSpecial) {
    pSelection = lowercaseAlphabetChar + uppercaseAlphabetChar + numericChar + specialChar;
    generatePWD();
  }
//Lowercase and Numbers
  else if (wantsLower && !wantsUpper && wantsNumbers && !wantsSpecial) {
    pSelection = lowercaseAlphabetChar + numericChar;
    generatePWD();
  }
//Lowercase and Numbers and Special
  else if (wantsLower && !wantsUpper && wantsNumbers && wantsSpecial) {
    pSelection = lowercaseAlphabetChar + numericChar + specialChar;
    generatePWD();
  }
// Lowercase and Special
  else if (wantsLower && !wantsUpper && !wantsNumbers && wantsSpecial) {
    pSelection = lowercaseAlphabetChar + specialChar;
    generatePWD();
  }
// Uppercase
  else if (!wantsLower && wantsUpper && !wantsNumbers && !wantsSpecial) {
    pSelection = uppercaseAlphabetChar;
    generatePWD();
  }
// UpperCase and Numbers
  else if (!wantsLower && wantsUpper && wantsNumbers && !wantsSpecial) {
    pSelection = uppercaseAlphabetChar + numericChar;
    generatePWD();
  }

// Uppercase and Numbers and Special
  else if (!wantsLower && wantsUpper && wantsNumbers && wantsSpecial) {
    pSelection = uppercaseAlphabetChar + numericChar + specialChar;
    generatePWD();
  }
// Uppercase and Special
  else if (!wantsLower && wantsUpper && !wantsNumbers && wantsSpecial) {
    pSelection = uppercaseAlphabetChar + specialChar;
    generatePWD();
  }
// Numbers
  else if (!wantsLower && !wantsUpper && wantsNumbers && !wantsSpecial) {
    pSelection = numericChar;
    generatePWD();
  }
// Numbers and Special
  else if (!wantsLower && !wantsUpper && wantsNumbers && wantsSpecial) {
    pSelection = numericChar + specialChar;
    generatePWD();
  }
// Special
  else if (!wantsLower && !wantsUpper && !wantsNumbers && wantsSpecial) {
    pSelection = specialChar;
    generatePWD();
  }
  document.getElementById("password").innerHTML = password;
  console.log(password);

});

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Password Generator</title>
    <link rel="stylesheet" href="assets/style.css" />
  </head>
  <body>
    <div class="wrapper">
      <header>
        <h1>Password Generator</h1>
      </header>
      <div class="card">
        <div class="card-header">
          <h2>Generate a Password</h2>
        </div>
        <div class="card-body">
          <textarea
            readonly
            id="password"
            placeholder="Your Secure Password"
            aria-label="Generated Password"
          ></textarea>
        </div>
        <div class="card-footer">
          <button id="generate" class="btn">Generate Password</button>
        </div>
      </div>
    </div>





    <script src="assets/script.js"></script>
  </body>
</html>

numericChar 需要是一个字符串,即 "1234567890" - 事实上,你已经用数字初始化了它。

如果将变量的声明改为

var numericChar = "1234567890";

那么应该可以了:)