[SOLVED] Random password generator returning orderly password instead of random password. (JavaScript)

Issue

This Content is from Stack Overflow. Question asked by DylanSchmidt

I’m working on a random password generator for a class and almost have it figured out but instead of returning the password as random it comes back orderly…(“abcdefghijklmnopqrstuvwxyz12343..”)
Judging by other peoples mistakes I’m guessing its in my random variable.

Any help is much appreciated! 🙂

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

function getPass() {
var upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var upperArray = upperCase.split("");
var lowerCase = "abcdefghijklmnopqrstuvwxyz";
var lowerArray = lowerCase.split("");
var num = "0123456789";
var numArray = num.split("")
var specials = "!@#$%^&*()_+?<>";
var specialsArray = specials.split("");
allCharacters = [];


password = "";

var length = prompt("How many characters would you like your password to be?            Min-8 Max-128");

if (length < 8 || length > 128) {
    alert("Password must be between defined range.");
    getPass()
  }
  if (confirm("Do you want uppercase letters?")) {
    allCharacters.push(upperArray);
  }
  if (confirm("Do you want lowercase letters?")){
    allCharacters.push(lowerArray);
  }
  if (confirm("Do you want numbers?")){
    allCharacters.push(numArray);
  }
  if (confirm("Do you want special characters ie. !, @, #, $, ?")){
    allCharacters.push(specialsArray);
  }
  for (var i = 0; i < length; i++){
    var random = Math.floor(Math.random() * Math.floor(allCharacters.length));
    password += allCharacters[random];
  }
  
  return password;

}

generateBtn.addEventListener("click", putPass);**strong text**


function putPass() {
    var password = getPass();
    var passwordText = document.querySelector("#password");
    passwordText.value = password;
    }



Solution

Replace all your

allCharacters.push(upperArray);

with

allCharacters = allCharacters.concat(upperArray);

(and similarily to that for other groups)

The problem with your approach is that you are putting arrays into the allCharacters array instead of putting contents of these arrays into that array. And even if every group is selected, instead of multiple different characters, the candidate array contains just 4 other arrays you then choose from.

Note that there’s still an issue in your code, if no group is selected then the password is not generated correctly. You could make one of groups not optional.

    function getPass() {
    var upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var upperArray = upperCase.split("");
    var lowerCase = "abcdefghijklmnopqrstuvwxyz";
    var lowerArray = lowerCase.split("");
    var num = "0123456789";
    var numArray = num.split("")
    var specials = "!@#$%^&*()_+?<>";
    var specialsArray = specials.split("");
    allCharacters = [];


    password = "";

    var length = prompt("How many characters would you like your password to be?            Min-8 Max-128");

    if (length < 8 || length > 128) {
        alert("Password must be between defined range.");
        getPass()
      }
      if (confirm("Do you want uppercase letters?")) {
        allCharacters = allCharacters.concat(upperArray);
      }
      if (confirm("Do you want lowercase letters?")){
        allCharacters = allCharacters.concat(lowerArray);
      }
      if (confirm("Do you want numbers?")){
        allCharacters = allCharacters.concat(numArray);
      }
      if (confirm("Do you want special characters ie. !, @, #, $, ?")){
        allCharacters = allCharacters.concat(specialsArray);
      }
      for (var i = 0; i < length; i++){
        var random = Math.floor(Math.random() * Math.floor(allCharacters.length));
        password += allCharacters[random];
      }
      
      return password;

    }
  
window.alert( `The random password is ${getPass()}` );


This Question was asked in StackOverflow by DylanSchmidt and Answered by Wiktor Zychla It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?