Last active
February 7, 2021 10:48
-
-
Save darkcris1/6ff629704d149908ea7e12cf34709777 to your computer and use it in GitHub Desktop.
Simple 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 generatePassword(length = 50) { | |
if (length < 5) throw new Error('Length must be minimum of 5') | |
const chars = 'abcdefghijklmopqrstnuvwxyz-[]{}()*-+,./\\1234567890' | |
let result = '' | |
for (let i = 0; i < length; i++) { | |
const randomChars = chars[Math.floor(Math.random() * chars.length)] | |
result += randomChars | |
} | |
return result | |
} | |
console.log(generatePassword()) // y.7xu2x+wcmo(ji/rzv]zsq6421c8/\zz{]pd31qvqyr1at.}p | |
console.log(generatePassword(10)) // (v*2x}w1cd | |
console.log(generatePassword(5)) // 0auy- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment