1
1
const passwordElem = document . getElementById ( "password" ) ,
2
2
lengthElem = document . querySelector ( "#length" ) ,
3
3
lengthTextElem = document . querySelector ( "#lengthText" ) ,
4
- addSymbolsElem = document . querySelector ( "#symbols" ) ,
5
- addNumbersElem = document . querySelector ( "#numbers" ) ,
6
- addLowerCaseElem = document . querySelector ( "#lowercase" ) ,
7
- addUpperCaseElem = document . querySelector ( "#uppercase" ) ,
8
- addSimilarElem = document . querySelector ( "#similar" ) ,
9
4
copyButtonElem = document . querySelector ( ".copy" ) ,
10
5
copyButtonTextElem = document . querySelector ( ".copy span" ) ;
11
6
@@ -19,41 +14,29 @@ const characters = {
19
14
numbers : "0123456789"
20
15
} ;
21
16
22
- const showSymbol = ( ) => {
23
- const newPassword = [ ] ;
24
- hideCopiedMessage ( ) ;
25
-
26
- const lengthValue = + lengthElem . value ,
27
- hasSymbols = addSymbolsElem . checked ,
28
- hasNumbers = addNumbersElem . checked ,
29
- hasLowerCase = addLowerCaseElem . checked ,
30
- hasUpperCase = addUpperCaseElem . checked ,
31
- hasSimilar = addSimilarElem . checked ;
17
+ const generatePassword = ( ) => {
18
+ const passwordArray = [ ] ;
19
+ const lengthRequired = + lengthElem . value ;
20
+ const options = Array . prototype . slice . call ( checkboxes ) ;
32
21
33
- const options = [
34
- hasSymbols ,
35
- hasNumbers ,
36
- hasLowerCase ,
37
- hasUpperCase ,
38
- hasSimilar
39
- ] ;
22
+ const noOptionsChecked = options . every ( ( option ) => ! option . checked ) ;
40
23
41
- const noOptionChecked = options . every ( ( option ) => ! option ) ;
24
+ hideCopiedMessage ( ) ;
42
25
43
- if ( noOptionChecked ) {
26
+ if ( noOptionsChecked ) {
44
27
passwordElem . value = "select an option" ;
45
28
} else {
46
- while ( newPassword . length < lengthValue ) {
47
- for ( const box in checkboxes ) {
48
- if ( checkboxes [ box ] . checked )
49
- newPassword . push ( getRandomCharacter ( checkboxes [ box ] . name ) ) ;
29
+ while ( passwordArray . length < lengthRequired ) {
30
+ for ( const i in checkboxes ) {
31
+ if ( checkboxes [ i ] . checked )
32
+ passwordArray . push ( getRandomCharacter ( checkboxes [ i ] . name ) ) ;
50
33
}
51
34
}
52
35
53
- const shuffledFinalPassword = newPassword
36
+ const shuffledFinalPassword = passwordArray
54
37
. sort ( ( a , b ) => 0.5 - Math . random ( ) )
55
38
. join ( "" )
56
- . slice ( 0 , lengthValue ) ;
39
+ . slice ( 0 , lengthRequired ) ;
57
40
58
41
passwordElem . value = shuffledFinalPassword ;
59
42
}
@@ -78,4 +61,4 @@ copyButtonElem.addEventListener("click", () => {
78
61
showCopiedMessage ( ) ;
79
62
} ) ;
80
63
81
- lengthElem . addEventListener ( "input" , ( ) => showSymbol ( ) ) ;
64
+ lengthElem . addEventListener ( "input" , ( ) => generatePassword ( ) ) ;
0 commit comments