Skip to content

Instantly share code, notes, and snippets.

@blackavec
Last active April 3, 2018 06:02
Show Gist options
  • Save blackavec/de80f4ea9f467b6231b578c101f41f98 to your computer and use it in GitHub Desktop.
Save blackavec/de80f4ea9f467b6231b578c101f41f98 to your computer and use it in GitHub Desktop.
create Random string from input in length of 8 char
function getRandomIntInclusive (min, max) {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min + 1)) + min
}
async function codeGenerator (input) {
const inputLengthLimit = 5
let codeStarter = input.substring(0, inputLengthLimit).toLowerCase()
if (codeStarter.length < inputLengthLimit) {
const starterFillerCount = inputLengthLimit - codeStarter.length
// fill the gap with random number incase input was les than limit of chars
codeStarter += getRandomIntInclusive(
+`1${'0'.repeat(starterFillerCount - 1)}`,
+`9${'9'.repeat(starterFillerCount - 1)}`
)
}
return `${codeStarter}${getRandomIntInclusive(100, 999)}`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment