I am creating a random password generator. In the first function I returned all the user input from prompts and confirm, for the length of password and characters to be used. In the second function I tried to reference the first function so as to grab the user input of the password length and use it in my second function, to generate the random password. Unfortunately when I did this it ran through the first function's code twice before giving me the password. How can I grab this user selected value of length and use it in the second function without having the first function run twice?
I tired inputting the first function into my second function as a variable, but it just made the code run twice.
document.getElementById('generate').addEventListener('click', generateOptions);
function generateOptions() {
let length = (prompt("Enter how many characters you would like your password to be. Choose between 8 and 128 characters."));
//Loop if user selects incorrect number.
while (length < 8 || length > 128) {
length = (prompt("Password length must be between 8 and 128 characters, please try again."));
}
//Confirm which type of characters to use in password.
let upperCase = confirm("Would you like to include uppercase letters in your password?");
let lowerCase = confirm("Would you like to include lowercase letters in your password?");
let number = confirm("Would you like to include numbers in your password?");
let special = confirm("Would you like to include special characters in your password?");
//Loop if user doesn't select any character types.
while (!(upperCase || lowerCase || number || special)) {
alert("Please select at least one character type.");
upperCase = confirm("Would you like to include uppercase letters in your password?");
lowerCase = confirm("Would you like to include lowercase letters in your password?");
number = confirm("Would you like to include numbers in your password?");
special = confirm("Would you like to include special characters in your password?");
}
//Return values that the user selected.
let options = [ length, upperCase, lowerCase, number, special ];
return options;
};
//Password Generator
function generatePassword() {
var v = generateOptions();
let length = v[0];
let password = "";
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*>< _-=";
for (var i = 0, n = charset.length; i < length; i ) {
password = charset.charAt(Math.floor(Math.random() * n));
}
return password;
};
CodePudding user response:
Why don't you just call the second function in the first one and give the options as a parameter?
like this:
function generateOptions() {
// generate option values
let options = [
length,
upperCase,
lowerCase,
number,
special];
// call generatePassword with options as parameter
generatePassword(options)
};
function generatePassword(options) {
// accept the options parameter
let length = options[0];
let password = "";
let charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*>< _-=";
for (let i = 0, n = charset.length; i < length; i ) {
password = charset.charAt(Math.floor(Math.random() * n));
}
return password;
};
CodePudding user response:
how to fix it
change this :
document.getElementById('generate').addEventListener('click', generateOptions);
to this:
document.getElementById('generate').addEventListener('click', generatePassword);
what is problem
first time you execute generateOptions
when user clicks on the button and then one more time in your second function, you execute the generateOptions
and store value to v
first time
document.getElementById('generate').addEventListener('click', generateOptions);
second time:
var v = generateOptions();
last note
!don't use var use const or let instead.
I believe you can use Destructuring assignment and const like this in your code instead of var v = generateOptions();
:
const [length, upperCase, lowerCase, number, special ] = generateOptions();
CodePudding user response:
This is just an example, not the best way to do so, perhaps.
You can read about maps here if you want to make those values properties of the function and avoid name clashes. But you don't have to use a Map. It all depends on the rest of your code.
But the fundamental issue you have now is you are returning values to "nowhere" because they are not being captured/stored in a variable or immediately used.
document.getElementById('generate').addEventListener('click', () => {globOptions= generateOptions();});
document.getElementById('generate').addEventListener('click', generatePassword);
globOptions = new Array();
function generateOptions(){
let length = (prompt("Enter how many characters you would like your password to be. Choose between 8 and 128 characters."));
//Loop if user selects incorrect number.
while(length < 8 || length > 128){
length = (prompt("Password length must be between 8 and 128 characters, please try again."));
}
//Confirm which type of characters to use in password.
let upperCase = confirm("Would you like to include uppercase letters in your password?");
let lowerCase = confirm("Would you like to include lowercase letters in your password?");
let number = confirm("Would you like to include numbers in your password?");
let special = confirm("Would you like to include special characters in your password?");
//Loop if user doesn't select any character types.
while (!(upperCase || lowerCase || number || special)){
alert("Please select at least one character type.");
upperCase = confirm("Would you like to include uppercase letters in your password?");
lowerCase = confirm("Would you like to include lowercase letters in your password?");
number = confirm("Would you like to include numbers in your password?");
special = confirm("Would you like to include special characters in your password?");
}
//Return values that the user selected.
let options =[
length,
upperCase,
lowerCase,
number,
special];
return options;
};
//Password Generator
function generatePassword(){
//var v = generateOptions();
var v = globOptions;
console.log(v);
let length = v[0];
let password = "";
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*>< _-=";
for (var i = 0, n = charset.length; i < length; i ){
password = charset.charAt(Math.floor(Math.random() * n));
}
return password;
};
<button id="generate">Generate Options</button>
<button id="generate">Generate Password</button>