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;
}
CodePudding user response:
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()}` );
CodePudding user response:
allCharacters.push(specialsArray)
doesn't work as you intended
You just push entire array into the existing one
const a = [1, 2, 3]
const b = [4, 5, 6]
a.push(b)
console.log(a)
But you wanted to push the items instead:
const a = [1, 2, 3]
const b = [4, 5, 6]
a.push(...b)
console.log(a)
Entire code:
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);
function putPass() {
var password = getPass();
console.log(password)
var passwordText = document.querySelector("#password");
passwordText.value = password;
}
<button id="generate">generate</button>
<p id="password"></p>
P.S.
Note that your code isn't cryptographically safe and shouldn't be used for real password generation
CodePudding user response:
I was pushing the entire array using:
allCharacters.push(upperArray);
instead of
allCharacters.push...upperArray);
Thank you everyone for the help.