Last active
September 1, 2022 05:04
-
-
Save michaelwclark/fc06a9c029178f5e6c2f6d31412a29d4 to your computer and use it in GitHub Desktop.
RandomPassword
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
| const randomLetter = () => { | |
| const alphabet = 'abcdefghijklmnopqrstuvwxyz'; | |
| return alphabet[Math.floor(Math.random() * alphabet.length)]; | |
| } | |
| const randomNumber = () => { | |
| return Math.floor(Math.random() * 10); | |
| } | |
| const randomSymbol = () => { | |
| const symbols = '!@#$%^&*(){}[]=<>/,.'; | |
| return symbols[Math.floor(Math.random() * symbols.length)]; | |
| } | |
| const randomPassword = (length = 8) => { | |
| const passwordCharacters = []; | |
| for (let i = 0; i < length; i++) { | |
| const randomNum = Math.floor(Math.random() * 30) | |
| if (randomNum < 10) { | |
| const shouldUpperCase = Math.floor(Math.random() * 3) % 2 == 0; | |
| passwordCharacters.push(shouldUpperCase ? randomLetter().toUpperCase() : randomLetter()); | |
| } else if (randomNum < 20) { | |
| passwordCharacters.push(randomNumber()); | |
| } else{ | |
| passwordCharacters.push(randomSymbol()); | |
| } | |
| } | |
| return passwordCharacters.join(''); | |
| } | |
| console.log(randomPassword(32)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment