@@ -9,10 +9,18 @@ const passwordElem = document.getElementById("password"),
9
9
copyButtonElem = document . querySelector ( ".copy" ) ,
10
10
copyButtonTextElem = document . querySelector ( ".copy span" ) ;
11
11
12
- const newPassword = [ ] ;
12
+ const checkboxes = document . querySelectorAll ( "input[type=checkbox]" ) ;
13
13
14
- function showSymbol ( ) {
15
- newPassword . length = 0 ;
14
+ const characters = {
15
+ symbols : "!@#$%^&*(){}[]=<>/,." ,
16
+ similar : "il1Lo0O" ,
17
+ uppercase : "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ,
18
+ lowercase : "abcdefghijklmnopqrstuvwxyz" ,
19
+ numbers : "0123456789"
20
+ } ;
21
+
22
+ const showSymbol = ( ) => {
23
+ const newPassword = [ ] ;
16
24
hideCopiedMessage ( ) ;
17
25
18
26
const lengthValue = + lengthElem . value ,
@@ -22,40 +30,42 @@ function showSymbol() {
22
30
hasUpperCase = addUpperCaseElem . checked ,
23
31
hasSimilar = addSimilarElem . checked ;
24
32
25
- const checkboxesCounter =
26
- hasSymbols + hasNumbers + hasLowerCase + hasUpperCase + hasSimilar ;
33
+ const options = [
34
+ hasSymbols ,
35
+ hasNumbers ,
36
+ hasLowerCase ,
37
+ hasUpperCase ,
38
+ hasSimilar
39
+ ] ;
40
+
41
+ const noOptionChecked = options . every ( ( option ) => ! option ) ;
27
42
28
- if ( checkboxesCounter === 0 ) {
43
+ if ( noOptionChecked ) {
29
44
passwordElem . value = "select an option" ;
30
45
} else {
31
46
while ( newPassword . length < lengthValue ) {
32
- if ( hasSymbols ) newPassword . push ( getRandomSymbol ( ) ) ;
33
- if ( hasNumbers ) newPassword . push ( getRandomFromChartCode ( 10 , 48 ) ) ;
34
- if ( hasLowerCase ) newPassword . push ( getRandomFromChartCode ( 26 , 97 ) ) ;
35
- if ( hasUpperCase ) newPassword . push ( getRandomFromChartCode ( 26 , 65 ) ) ;
36
- if ( hasSimilar ) newPassword . push ( getRandomSimilar ( ) ) ;
47
+ for ( const box in checkboxes ) {
48
+ if ( checkboxes [ box ] . checked )
49
+ newPassword . push ( getRandomCharacter ( checkboxes [ box ] . name ) ) ;
50
+ }
37
51
}
38
- const generatedPassword = newPassword . join ( "" ) ;
39
- const finalPassword = generatedPassword . slice ( 0 , lengthValue ) ;
40
- passwordElem . value = finalPassword ;
41
- }
42
52
43
- lengthTextElem . textContent = lengthElem . value ;
44
- }
53
+ const shuffledFinalPassword = newPassword
54
+ . sort ( ( a , b ) => 0.5 - Math . random ( ) )
55
+ . join ( "" )
56
+ . slice ( 0 , lengthValue ) ;
45
57
46
- function getRandomFromChartCode ( quantity , startFrom ) {
47
- return String . fromCharCode ( Math . floor ( Math . random ( ) * quantity ) + startFrom ) ;
48
- }
58
+ passwordElem . value = shuffledFinalPassword ;
59
+ }
49
60
50
- function getRandomSymbol ( ) {
51
- const symbols = "!@#$%^&*(){}[]=<>/,." ;
52
- return symbols [ Math . floor ( Math . random ( ) * symbols . length ) ] ;
53
- }
61
+ lengthTextElem . textContent = lengthElem . value ;
62
+ } ;
54
63
55
- function getRandomSimilar ( ) {
56
- const similar = "il1Lo0O" ;
57
- return similar [ Math . floor ( Math . random ( ) * similar . length ) ] ;
58
- }
64
+ const getRandomCharacter = ( optionName ) => {
65
+ return characters [ optionName ] [
66
+ Math . floor ( Math . random ( ) * characters [ optionName ] . length )
67
+ ] ;
68
+ } ;
59
69
60
70
const showCopiedMessage = ( ) => ( copyButtonTextElem . style . display = "block" ) ;
61
71
const hideCopiedMessage = ( ) => ( copyButtonTextElem . style . display = "none" ) ;
0 commit comments