-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
60 lines (49 loc) · 1.49 KB
/
app.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 upperSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const lowerSet = "abcdefghijklmnopqrstuvwxyz"
const numberSet = "0123456789"
const symbolSet = "~!@#$%^&*()_+/?-"
//selectors
const passBox = document.getElementById("pass-box")
const totalChar = document.getElementById("total-char")
const upperInput = document.getElementById("upper-case")
const lowerInput = document.getElementById("lower-case")
const numberInput = document.getElementById("numbers")
const symbolInput = document.getElementById("symbols")
const getRandomData = (dataSet) => {
return dataSet[Math.floor(Math.random() * dataSet.length)]
}
const generatePassword = (password = "") => {
if(upperInput.checked) {
password += getRandomData(upperSet)
}
if(lowerInput.checked) {
password += getRandomData(lowerSet)
}
if(numberInput.checked) {
password += getRandomData(numberSet)
}
if(symbolInput.checked) {
password += getRandomData(symbolSet)
}
if(password.length < totalChar.value) {
return generatePassword(password)
}
passBox.innerText = truncateString(password, totalChar.value)
}
generatePassword();
document.getElementById("btn").addEventListener(
"click",
function() {
generatePassword();
}
)
function truncateString(str, num) {
if(str.length > num) {
let subStr = str.substring(0, num);
return subStr;
}
else
{
return str;
}
}