-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpass.js
63 lines (61 loc) · 1.89 KB
/
pass.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
61
62
63
let lowerCase ="abcdefghijklmnopqrstuvwxyz";
let nums="1234567890";
let upperCase = lowerCase.toUpperCase();
let symbols='.!$#&^*$?';
var random = document.querySelector("input[type='text']");
let btn=document.querySelector("a.btn");
let checkBox=document.querySelectorAll("input[type='checkbox']");
let selectedNumber=document.querySelector("select");
let obj ={
'upper':upperCase,
'lower':lowerCase,
'num':nums,
'symbol':symbols
}
// reset random value
random.value='';
btn.onclick =function () {
let requirdLetter=[],rand='';
let length=selectedNumber.value;
let arr=[];
let format=[];let i=0
checkBox.forEach(el => {
if(el.checked) {
requirdLetter.push(obj[el.dataset.value]);
arr.push(el.dataset.value);
format[i]=1;i++;
}
});
// confirm that word consists of all option selected
let prefixSum=0;
let copyOfLength=length;
for(let i=0;i<format.length-1;++i) {
format[i]+=Math.ceil(Math.random() * (length-(format.length+prefixSum)))
prefixSum+=format[i]-1;
copyOfLength-=format[i];
}
format[format.length-1]+=copyOfLength-1;
//format the password in way all option is in it
if(arr.length==1) {
for (let i=1; i <=length; ++i) {
rand+= requirdLetter[0][Math.trunc(Math.random() * requirdLetter[0].length)];
}
}
else {
for (let i=0; i <format.length; ++i) {
for(let j=0;j<format[i];j++) {
rand+= requirdLetter[i][Math.trunc(Math.random() * requirdLetter[i].length)];
}
}
}
//Now the word is sorted like that asd1234%^* => char num symbol
// for random sort
rand=Array.from(rand);
let finalAns='';
for (let i=1; i <=length; ++i) {
let j=Math.trunc(Math.random() * rand.length);
finalAns+= rand[j];
rand.splice(j,1)
}
random.value=finalAns;
}