1- var len = document . getElementById ( "length" ) ;
1+ // Functions for generating random number lowercase uppercase letters , symbols
22
3+ /* Math.random method genrate a random floating-point numbers
4+ Math.floor() function returns the largest integer less than or equal to a given number.
5+ For generating a random uppercase lowercase text random numbers symbols we use Charcode
6+ http://stevehardie.com/2009/09/character-code-list-char-code/ */
37
4- //PASSWORD GENERATOR FUNCTION
5- function getPassword ( ) {
6- const chars = '0123456789/%?@$#' ;
7- var lengthOfPassword = len . value ;
8- flag = 0 ;
9- if ( lengthOfPassword == '' ) {
10- window . alert ( 'Please Enter the length' ) ;
11- flag = 1 ;
12- }
13-
14- if ( lengthOfPassword <= 4 && flag == 0 ) {
15- window . alert ( 'Password length cannot be less than 4 characters' ) ;
16- }
17-
18-
19- //console.log(lengthOfPassword);
20- const clength = chars . length ;
21- let password = '' ;
22- let p = 0 ;
23- console . log ( randomWord ( chars . length ) )
24- while ( p < lengthOfPassword ) {
25- password = password . concat ( randomWord ( chars . length ) ) ;
26- password = password . concat ( chars [ randomNumber ( clength ) ] ) ;
27- p ++ ;
28- }
29- password = password . substring ( 0 , p ) ;
30- document . getElementById ( 'password' ) . value = password ;
31-
32- }
33-
34- function randomNumber ( e ) {
35- return Math . floor ( Math . random ( ) * e ) ;
36- }
37-
38- function randomWord ( ) {
39- const number = randomNumber ( words . length ) ;
40- return words [ number ] ;
41- }
42- //////////////////////////////////////
43-
44- //COPY BUTTON SCRIPT
45- function Copy ( ) {
46- // Get the text field
47- var copyText = document . getElementById ( "password" ) ;
48-
49- // Select the text field
50- copyText . select ( ) ;
51- copyText . setSelectionRange ( 0 , 99999 ) ; // For mobile devices
52-
53- // Copy the text inside the text field
54- navigator . clipboard . writeText ( copyText . value ) ;
8+ function getRandomLower ( ) {
9+ return String . fromCharCode ( Math . floor ( Math . random ( ) * 26 ) + 97 ) ;
10+ }
5511
56- // Alert the copied text
57- alert ( "Copied the text: " + copyText . value ) ;
12+ function getRandomUpper ( ) {
13+ return String . fromCharCode ( Math . floor ( Math . random ( ) * 26 ) + 65 ) ;
5814 }
59-
60- ////////////////////////////////////////////////////////
61-
62-
63- const words = [ 'BOB' , 'BAM' , 'BND' , 'XOF' , 'BIF' ,
64- 'KHR' , 'XAF' , 'CVE' , 'XAF' , 'CLP' , 'COP' , 'KMF' ,
65- 'CRC' , 'CUP' , 'CDF' , 'DKK' , 'DJF' , 'XCD' , 'DOP' ,
66- 'EGP' , 'XAF' , 'ERN' , 'ETB' , 'FJD' , 'XAF' , 'GEL' ,
67- 'mud' , 'sky' , 'grew' , 'hard' , 'ill' , 'frame' ,
68- 'XCD' , 'GNF' , 'GTQ' , 'XOF' , 'GYD' , 'HTG' , 'HNL' ,
69- 'GNF' , 'HNL' , 'ISK' , 'IRR' , 'Japan' , 'Australia' , 'Afghanistan' ,
70- 'Albania' , 'Algeria' , 'Brunei' , 'Barbados' , 'Chile' , 'Dominica' , 'Zambia' ,
71- 'Aromatic' , 'Hearty' , 'Roux' , 'Victuals' , 'Chiffonade' , 'Divine' , 'Omakase' ,
72- 'Sapid' , 'Earthy' , 'Fresh' , 'Smoky' , 'Sweet' , 'Sour' , 'Airy' ,
73- 'Chewy' , 'Creamy' , 'Crumbly' , 'Blackened' , 'Broiled' , 'Glazed' , 'Sauteed' ,
74- 'Basement' , 'Den' , 'Hall' , 'Porch' , 'Yard' , 'Counter' , 'Mirror' ,
75- 'Sink' , 'Armchair ' , 'Rug' , 'Glass' , 'Napkin' , 'Stove' , 'Microwave' ,
76- 'Toaster' , 'dig' , 'new' , 'rest' , 'play' , 'win' , 'strong' ] ;
15+
16+ function getRandomNumber ( ) {
17+ return + String . fromCharCode ( Math . floor ( Math . random ( ) * 10 ) + 48 ) ;
18+ }
19+
20+ function getRandomSymbol ( ) {
21+ const symbols = "!@#$%^&*(){}[]=<>/,." ;
22+ return symbols [ Math . floor ( Math . random ( ) * symbols . length ) ] ;
23+ }
24+
25+ // adding a all functions into a object called randomFunc
26+ const randomFunc = {
27+ lower : getRandomLower ,
28+ upper : getRandomUpper ,
29+ number : getRandomNumber ,
30+ symbol : getRandomSymbol ,
31+ } ;
32+
33+ // adding a click event listner to generate button
34+ const generate = document . getElementById ( "generateBtn" ) ;
35+ generate . addEventListener ( "click" , ( ) => {
36+ const length = document . getElementById ( "Passwordlength" ) . value ;
37+ const hasUpper = document . getElementById ( "uppercase" ) . checked ;
38+ const hasLower = document . getElementById ( "lowercase" ) . checked ;
39+ const hasNumber = document . getElementById ( "numbers" ) . checked ;
40+ const hasSymbol = document . getElementById ( "symbols" ) . checked ;
41+ const result = document . getElementById ( "PasswordResult" ) ;
42+ result . innerText = generatePassword (
43+ hasLower ,
44+ hasUpper ,
45+ hasNumber ,
46+ hasSymbol ,
47+ length
48+ ) ;
49+ // console.log(hasLower, hasUpper, hasNumber, hasSymbol);
50+ } ) ;
51+
52+ // function for generating random password
53+ function generatePassword ( lower , upper , number , symbol , length ) {
54+ let generatedPassword = "" ;
55+ const typesCount = lower + upper + number + symbol ;
56+ // filter out unchecked types
57+ const typesArr = [ { lower } , { upper } , { number } , { symbol } ] . filter (
58+ ( item ) => Object . values ( item ) [ 0 ]
59+ ) ;
60+ // console.log(typesArr);
61+
62+ // creating a loop for calling generator function for each type
63+ for ( let i = 0 ; i < length ; i += typesCount ) {
64+ typesArr . forEach ( ( type ) => {
65+ const funcName = Object . keys ( type ) [ 0 ] ;
66+ generatedPassword += randomFunc [ funcName ] ( ) ;
67+ } ) ;
68+ }
69+
70+ // slicing password from 0 to length
71+ const finalPassword = generatedPassword . slice ( 0 , length ) ;
72+ return finalPassword ;
73+ }
74+
75+ // copy to clipboard
76+ let button = document . getElementById ( "clipboardBtn" ) ;
77+ // add click event listner on button
78+ button . addEventListener ( "click" , ( e ) => {
79+ e . preventDefault ( ) ;
80+ // execute command for copy text by selecting textarea text with id
81+ document . execCommand (
82+ "copy" ,
83+ false ,
84+ document . getElementById ( "PasswordResult" ) . select ( )
85+ ) ;
86+ } ) ;
87+
0 commit comments