I'm making a password generator with JavaScript and it works by getting a number from an input and it generates a password with the length that it got from the input. I added three checkboxes in HTML which represent caps, numbers and symbols. If you leave the checkboxes unchecked, it will generate the password using only lowercase letters, but if you mark the caps checkbox, it will use both lowercase and uppercase letters to create the password and it works the same way with the other checkboxes.
To evaluate which checkboxes were checked I had to use plenty of if statements, here is my code:
if (document.getElementById('caps').checked === true &&
document.getElementById('numbers').checked === false &&
document.getElementById('symbols').checked === false)
{
const charsArray = lowercase.concat(caps);
password = GeneratePassword(passwordLength, charsArray);
}
else if (document.getElementById('caps').checked === false &&
document.getElementById('numbers').checked === true &&
document.getElementById('symbols').checked === false)
{
const charsArray = lowercase.concat(numbers);
password = GeneratePassword(passwordLength, charsArray);
}
else if (document.getElementById('caps').checked === false &&
document.getElementById('numbers').checked === false &&
document.getElementById('symbols').checked === true)
{
const charsArray = lowercase.concat(symbols);
password = GeneratePassword(passwordLength, charsArray);
}
else if (document.getElementById('caps').checked === true &&
document.getElementById('numbers').checked === true &&
document.getElementById('symbols').checked === false)
{
const charsArray = lowercase.concat(caps).concat(numbers);
password = GeneratePassword(passwordLength, charsArray);
}
else if (document.getElementById('caps').checked === true &&
document.getElementById('numbers').checked === true &&
document.getElementById('symbols').checked === true)
{
const charsArray = lowercase.concat(caps).concat(numbers).concat(symbols);
password = GeneratePassword(passwordLength, charsArray);
}
else if (document.getElementById('caps').checked === false &&
document.getElementById('numbers').checked === true &&
document.getElementById('symbols').checked === true)
{
const charsArray = lowercase.concat(numbers).concat(symbols);
password = GeneratePassword(passwordLength, charsArray);
}
else if (document.getElementById('caps').checked === true &&
document.getElementById('numbers').checked === false &&
document.getElementById('symbols').checked === true)
{
const charsArray = lowercase.concat(caps).concat(symbols);
password = GeneratePassword(passwordLength, charsArray);
}
else if (document.getElementById('caps').checked === true &&
document.getElementById('numbers').checked === true &&
document.getElementById('symbols').checked === false)
{
const charsArray = lowercase.concat(caps);
password = GeneratePassword(passwordLength, charsArray);
}
else {
password = GeneratePassword(passwordLength, lowercase);
}
As you can see, it is very repetitive. I know that one of the fundamentals for writing good code is DRY, so I wanted to know if there is another way to do this without making it so repetitive. I know switch is an alternative to if statements but I think that wouldn't solve the repetitive issue.
If anybody can provide any help I will appreciate it very much :)
Aucun commentaire:
Enregistrer un commentaire