-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (47 loc) · 1.62 KB
/
index.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function generatePassword(
length,
includeLowerCase,
includeUpperCase,
includeNumbers,
includeSymbols
) {
const lowercaseChars = "abcdefghijklmnopqrstuvwxyz";
const uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const numbers = "0123456789";
const symbols = "!@#$%^&*()_+~`|}{[]:;?><,./-=";
let allowedChars = "";
let password = "";
allowedChars += includeLowerCase ? lowercaseChars : "";
allowedChars += includeUpperCase ? uppercaseChars : "";
allowedChars += includeNumbers ? numbers : "";
allowedChars += includeSymbols ? symbols : "";
if (length <= 0) {
return `(password length must be at least 1)`;
}
if (allowedChars.length === 0) {
return `(no characters allowed)`;
}
let result = document.getElementById("result");
for (let i = 0; i < length; i++) {
const randomNumber = Math.floor(Math.random() * allowedChars.length);
password += allowedChars[randomNumber];
}
result.innerText = `${password}`;
return password;
}
let button = document.getElementById("generate");
button.addEventListener("click", () => {
const passwordLength = Number(document.getElementById("length").value);
const includeLowerCase = document.getElementById("include-lowercase").checked;
const includeUpperCase = document.getElementById("include-uppercase").checked;
const includeNumbers = document.getElementById("include-numbers").checked;
const includeSymbols = document.getElementById("include-symbols").checked;
const pass = generatePassword(
passwordLength,
includeLowerCase,
includeUpperCase,
includeNumbers,
includeSymbols
);
console.log(`Password ${pass}`);
});