Created
          December 6, 2018 17:31 
        
      - 
      
- 
        Save isu3ru/0800cd5e9fbfd76999f267cdffd53a17 to your computer and use it in GitHub Desktop. 
    Password generator function in javascript
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function generate(size) { | |
| let config = { | |
| numbers: true, | |
| symbols: false | |
| }; | |
| // add symbols if needed | |
| // config.symbols = true; | |
| let alphabet_low = "abcdefghijklmnopqrstuvwxyz" | |
| let alphabet_up = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
| let numbers = "0123456789"; | |
| let symbols = "~!@#$%^&*()_+=-;:{}[]<>?/"; | |
| var availChars = alphabet_low + alphabet_up; // can't TURN OFF the alphabet | |
| if (config.numbers) { | |
| availChars = alphabet_low + numbers + alphabet_up; | |
| } | |
| if (config.symbols) { | |
| availChars += symbols; | |
| } | |
| // A reasonably "intuitive" way to generate a password: | |
| var charPos; | |
| var pwChar; | |
| var pwLength = 10; // Change for shorter or longer password | |
| var pw = ""; | |
| for (i = 0; i < pwLength; i++) { | |
| charPos = Math.floor(Math.random() * availChars.length); | |
| pwChar = availChars.charAt(charPos); | |
| pw = pw + pwChar; | |
| } | |
| return pw; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment