Reputation: 25
Okay now I have been able to solve the Length problem by adding the length property to the while loop. However, when i generate the noSpecialCharacter(), it gives me NaN in front of the generated password.
this is the function:
const noSpecialCharacters = () => {
let password;
for (let i = 0; i < askUser; i++) {
let result3 = characterSet[randomCharacter()];
password += result3;
}
return password;
}
let askUser;
// create a function for prompts
const prompts = () => {
const minLength = 8;
const maxLength = 128;
askUser = null
while (askUser === null || askUser.length < minLength || askUser.length > maxLength) {
// ask the user again
askUser = prompt('Enter a password character length. Character length must be at least 8 and between 128.');
let confirmSpecialCharacter;
// if password is between length range
if (askUser >= minLength && askUser <= maxLength) {
// ask for special characters
confirmSpecialCharacter = confirm('Would you like for your password to include special characters?');
// if yes, call combine random
let pass = confirmSpecialCharacter === true ? combineRandom() : noSpecialCharacters();
alert(pass);
}
}
return askUser;
}
prompts();
Upvotes: 1
Views: 82
Reputation: 1898
Since people solved your question by adding .length
to the askUser
, I'm here to suggest you to reconstruct the code from while
loop to recursive by keep calling the function itself until it passed the validation. (code untested)
with this method you can prevent the nested condition by return
early.
// create a function for prompts
const prompts = () => {
const minLength = 8;
const maxLength = 128;
// ask the user
let askUser = prompt('Enter a password character length. Character length must be at least 8 and between 128.');
// keep asking the function itself until it passed the validation
if (askUser.length <= minLength || askUser.length >= maxLength || askUser === null) {
return prompts()
}
// ask for special characters
let confirmSpecialCharacter = confirm('Would you like for your password to include special characters?');
let pass;
// if yes, call combine random
if (confirmSpecialCharacter) {
pass = combineRandom();
} else {
pass = noSpecialCharacters();
}
alert(pass);
return askUser;
}
With ternary condition you can get much more shorter code.
// if yes, call combine random
let pass = confirmSpecialCharacter ? combineRandom() : noSpecialCharacters();
Upvotes: 1
Reputation: 177
When you check the length, use askUser.length But with this logic, when the user input is correct on the first time, nothing in the wile loop will run.
A better approach is to initialize askUser = null and set the condition in while to:
while (askUser === null || askUser.length < minLength || askUser.length > maxLength)
To check first if the askUser is null
let askUser;
// create a function for prompts
const prompts = () => {
const minLength = 8;
const maxLength = 128;
askUser = null
while (askUser === null || askUser.length < minLength || askUser.length > maxLength) {
// ask the user again
askUser = prompt('Enter a password character length. Character length must be at least 8 and between 128.');
let confirmSpecialCharacter;
// if password is between length range
if (askUser.length >= minLength && askUser.length <= maxLength) {
// ask for special characters
confirmSpecialCharacter = confirm('Would you like for your password to include special characters?');
// if yes, call combine random
if (confirmSpecialCharacter === 'yes') {
let pass = combineRandom();
alert(pass);
} else {
let pass = noSpecialCharacters();
alert(pass);
}
}
}
return askUser;
}
prompts()
Upvotes: 2
Reputation: 286
In your while condition, you are not actually checking for askUser
length. Change it to this line: while (askUser.length < minLength || askUser === null || askUser.length > maxLength) { /...
And it will work.
Upvotes: 0