Last active
August 2, 2023 09:35
-
-
Save samarpanda/5ee4a9cbabb3a570de5c699e19618d99 to your computer and use it in GitHub Desktop.
Random password generator
This file contains 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 getRandomCharacter(charset = "" ){ | |
const randomIndex = Math.floor(Math.random() * charset.length); | |
return charset.charAt(randomIndex); | |
} | |
function generatePassword(length) { | |
const charsetAlphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$?"; | |
// const charsetNumbers = "0123456789"; | |
// const specialCharacters = "!@#$?" | |
let password = ""; | |
for (let i = 0; i < length; i++) { | |
password += getRandomCharacter(charsetAlphabets); | |
} | |
return password; | |
} | |
// Usage example: | |
const password = generatePassword(15); // Generate a 15-character password | |
console.log(password); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment