-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
56 lines (46 loc) · 1.49 KB
/
script.js
File metadata and controls
56 lines (46 loc) · 1.49 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
const upperSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const lowerSet = "abcdefghijklmnopqrstuvwxyz"
const numberSet = "1234567890"
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)
}
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;
}
}