-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
36 lines (32 loc) · 1.35 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const passwordLengthInput = document.getElementById("password-length");
const generatePasswordButton = document.getElementById("generate-password");
const generatedPassword = document.getElementById("generated-password");
const lowercaseChars = "abcdefghijklmnopqrstuvwxyz";
const uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const numberChars = "0123456789";
const symbolChars = "!@#$%^&*()_+-=[]{}|;:,.<>?";
generatePasswordButton.addEventListener("click", () => {
const passwordLength = passwordLengthInput.value;
const useLowercase = lowercaseChars.length > 0;
const useUppercase = uppercaseChars.length > 0;
const useNumbers = numberChars.length > 0;
const useSymbols = symbolChars.length > 0;
let password = "";
for (let i = 0; i < passwordLength; i++) {
let char = "";
if (useLowercase) {
char += lowercaseChars[Math.floor(Math.random() * lowercaseChars.length)];
}
if (useUppercase) {
char += uppercaseChars[Math.floor(Math.random() * uppercaseChars.length)];
}
if (useNumbers) {
char += numberChars[Math.floor(Math.random() * numberChars.length)];
}
if (useSymbols) {
char += symbolChars[Math.floor(Math.random() * symbolChars.length)];
}
password += char[Math.floor(Math.random() * char.length)];
}
generatedPassword.textContent = password;
});