Last active
April 3, 2018 06:02
-
-
Save blackavec/de80f4ea9f467b6231b578c101f41f98 to your computer and use it in GitHub Desktop.
create Random string from input in length of 8 char
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 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