-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (69 loc) · 2.04 KB
/
script.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
64
65
66
67
68
69
70
71
72
73
74
75
76
// SELECTORS
const pass_length = document.querySelector(".len");
const uppercase = document.querySelector(".upp");
const lowercase = document.querySelector(".low");
const numb = document.querySelector(".num");
const special = document.querySelector(".spe");
const generate = document.querySelector(".generate");
const code = document.querySelector(".code");
// character sets
const upper_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lower_set = "abcdefghijklmnopqustuvwxyz";
const number_set = "1234567890";
const special_set = "!@#$%^&()*+_-?/><";
// function to generate random character
const generate_char = (word_set) => {
return word_set[Math.floor(Math.random() * word_set.length)];
};
// genertae on the basis of selection
const create_pass = (ans = "") => {
if (uppercase.checked) {
ans = ans + generate_char(upper_set);
}
if (lowercase.checked) {
ans = ans + generate_char(lower_set);
}
if (numb.checked) {
ans = ans + generate_char(number_set);
}
if (special.checked) {
ans = ans + generate_char(special_set);
}
if (
!special.checked &&
!numb.checked &&
!lowercase.checked &&
!uppercase.checked
) {
code.textContent = `⚠️ select type of password by clicking checkbox`;
return;
}
if (ans.length < Number(pass_length.value)) {
return create_pass(ans);
}
if (ans.length >= Number(pass_length.value)) {
return ans;
}
};
// event listner to throw answer when button is clicked:
generate.addEventListener("click", function () {
const ans = create_pass();
if (Number(pass_length.value) > 15 || Number(pass_length.value) < 8) {
code.textContent = `⚠️ password length should be 8-14 word length`;
return;
}
if (
!special.checked &&
!numb.checked &&
!lowercase.checked &&
!uppercase.checked
) {
code.textContent = `⚠️ select type of password by clicking checkbox`;
return;
}
code.textContent = `Password generated: ${ans.substring(
0,
Number(pass_length.value)
)}`;
//console.log(ans.substring(0, Number(pass_length.value)));
});