-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpg.js
More file actions
71 lines (47 loc) · 1.75 KB
/
pg.js
File metadata and controls
71 lines (47 loc) · 1.75 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const resultEl = document.getElementById('result');
const lengthEl = document.getElementById('length');
const uppercaseEl = document.getElementById('uppercase');
const numbersEl = document.getElementById('numbers');
const specialEl = document.getElementById('special');
const generateBtn = document.getElementById('generate');
const charSets = {
lower: 'abcdefghijklmnopqrstuvwxyz',
upper: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
number: '0123456789',
special: '!@#$%^&*()_+-=[]{}|;:,.<>?'
};
function getRandomChar(str) {
return str[Math.floor(Math.random() * str.length)];
}
function generatePassword() {
const length = +lengthEl.value;
const includeUpper = uppercaseEl.checked;
const includeNumber = numbersEl.checked;
const includeSpecial = specialEl.checked;
let characterPool = charSets.lower;
let password = [];
password.push(getRandomChar(charSets.lower));
if (includeUpper) {
characterPool += charSets.upper;
password.push(getRandomChar(charSets.upper));
}
if (includeNumber) {
characterPool += charSets.number;
password.push(getRandomChar(charSets.number));
}
if (includeSpecial) {
characterPool += charSets.special;
password.push(getRandomChar(charSets.special));
}
const remainingLength = length - password.length;
for (let i = 0; i < remainingLength; i++) {
password.push(getRandomChar(characterPool));
}
for (let i = password.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[password[i], password[j]] = [password[j], password[i]];
}
resultEl.innerText = password.join('');
}
generateBtn.addEventListener('click', generatePassword);
generatePassword();