-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
60 lines (51 loc) · 1.56 KB
/
main.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
53
54
55
56
57
58
59
60
const lengthValShow = document.getElementById('lengthValue');
const lengthInput = document.getElementById('passLengthValue');
const display = document.getElementById('password');
const button = document.getElementById('btn');
function lengthShow() {
lengthValShow.innerHTML = lengthInput.value;
}
setInterval(lengthShow, 10);
const lowerCase = 'abcdefghijklmnopqrstuvwxyz';
const upperCase = lowerCase.toUpperCase();
const number = '0123456789';
const symble = '!@#$%^&*()_+-=[]{};\':"\\|,.<>/?';
const upperCaseInput = document.getElementById('upperCase');
const lowerCaseInput = document.getElementById('lowerCase');
const numberInput = document.getElementById('number');
const symbleInput = document.getElementById('symble');
const getRandomData = (dataSet) => {
return dataSet[Math.floor(Math.random() * dataSet.length)];
};
const passwordGenerate = (password = '') => {
if (upperCaseInput.checked) {
password += getRandomData(upperCase);
}
if (lowerCaseInput.checked) {
password += getRandomData(lowerCase);
}
if (numberInput.checked) {
password += getRandomData(number);
}
if (symbleInput.checked) {
password += getRandomData(symble);
}
if (password.length < lengthInput.value) {
return passwordGenerate(password);
}
document.getElementById('password').value = truncateString(
password,
lengthInput.value
);
console.log(password);
};
button.addEventListener('click', () => {
passwordGenerate();
});
function truncateString(str, num) {
if (str.length > num) {
return str.slice(0, num);
} else {
return str;
}
}