Created
October 27, 2023 03:15
-
-
Save harithzainudin/8577655e1f4c6d135dd5bb37bd82ea42 to your computer and use it in GitHub Desktop.
Function to generate random characters according to the length provided
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 generateUniqueID(length) { | |
const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; | |
let uniqueID = ''; | |
while (uniqueID.length < length) { | |
const randomIndex = Math.floor(Math.random() * characters.length); | |
const randomChar = characters.charAt(randomIndex); | |
// Make sure the generated character is not already in the uniqueID | |
if (!uniqueID.includes(randomChar)) { | |
uniqueID += randomChar; | |
} | |
} | |
return uniqueID; | |
} | |
const uniqueID = generateUniqueID(5); | |
console.log(uniqueID); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment