1
+ const resultElement = document . getElementById ( "result" ) ;
2
+ const lengthElement = document . getElementById ( "length" ) ;
3
+ const uppercaseElement = document . getElementById ( "uppercase" ) ;
4
+ const lowercaseElement = document . getElementById ( "lowercase" ) ;
5
+ const numbersElement = document . getElementById ( "numbers" ) ;
6
+ const symbolsElement = document . getElementById ( "symbols" ) ;
7
+ const generateElement = document . getElementById ( "generate" ) ;
8
+ const clipboardElement = document . getElementById ( "clipboard" ) ;
9
+
1
10
// Random functions
2
11
// fromCharCode: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode
3
12
// ASCII codes: https://www.w3schools.com/charsets/ref_html_ascii.asp
4
- const randomFunctions = {
5
- lower : getRandomLower ,
6
- upper : getRandomUpper ,
7
- number : getRandomSymbol ,
8
- symbol : getRandomSymbol ,
9
- } ;
10
-
11
13
const getRandomLower = ( ) =>
12
14
String . fromCharCode ( Math . floor ( Math . random ( ) * 26 ) + 97 ) ;
13
15
@@ -21,3 +23,54 @@ const getRandomSymbol = () => {
21
23
const symbols = "!@#$%^&*(){}[]=<>/,." ;
22
24
return symbols [ Math . floor ( Math . random ( ) * symbols . length ) ] ;
23
25
} ;
26
+
27
+ const randomFunctions = {
28
+ lower : getRandomLower ,
29
+ upper : getRandomUpper ,
30
+ number : getRandomSymbol ,
31
+ symbol : getRandomSymbol ,
32
+ } ;
33
+
34
+ clipboardElement . addEventListener ( "click" , ( ) => {
35
+ const password = resultElement . innerText ;
36
+ if ( ! password ) return ;
37
+ const textarea = document . createElement ( "textarea" ) ;
38
+ textarea . value = password ;
39
+ document . body . appendChild ( textarea ) ;
40
+ textarea . select ( ) ;
41
+ document . execCommand ( "copy" ) ;
42
+ textarea . remove ( ) ;
43
+ alert ( "Password copied to clipboard!" ) ;
44
+ } ) ;
45
+
46
+ generateElement . addEventListener ( "click" , ( ) => {
47
+ const length = + lengthElement . value ;
48
+ const hasLower = lowercaseElement . checked ;
49
+ const hasUpper = uppercaseElement . checked ;
50
+ const hasNumber = numbersElement . checked ;
51
+ const hasSymbol = symbolsElement . checked ;
52
+ resultElement . innerText = generatePassword (
53
+ hasLower ,
54
+ hasUpper ,
55
+ hasNumber ,
56
+ hasSymbol ,
57
+ length
58
+ ) ;
59
+ } ) ;
60
+
61
+ const generatePassword = ( lower , upper , number , symbol , length ) => {
62
+ let generatedPassword = "" ;
63
+ const typesCount = lower + upper + number + symbol ;
64
+ const typesArr = [ { lower } , { upper } , { number } , { symbol } ] . filter (
65
+ ( item ) => Object . values ( item ) [ 0 ]
66
+ ) ;
67
+ if ( typesCount === 0 ) return "" ;
68
+ for ( let i = 0 ; i < length ; i += typesCount ) {
69
+ typesArr . forEach ( ( type ) => {
70
+ const funcName = Object . keys ( type ) [ 0 ] ;
71
+ generatedPassword += randomFunctions [ funcName ] ( ) ;
72
+ } ) ;
73
+ }
74
+ const finalPassword = generatedPassword . slice ( 0 , length ) ;
75
+ return finalPassword ;
76
+ } ;
0 commit comments