Skip to content

Instantly share code, notes, and snippets.

@michaelwclark
Last active September 1, 2022 05:04
Show Gist options
  • Save michaelwclark/fc06a9c029178f5e6c2f6d31412a29d4 to your computer and use it in GitHub Desktop.
Save michaelwclark/fc06a9c029178f5e6c2f6d31412a29d4 to your computer and use it in GitHub Desktop.
RandomPassword
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