Here you are -code in ES6
-:
You need to separate numbers and characters,
But to generate random unique id you can refer to here
const makeid = () => {
let id = '';
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const nums = '0123456789'
for (let i = 0; i < 2; i++) {
id += chars[Math.floor(Math.random() * chars.length) + 1]
}
for (let i = 0; i < 3; i++) {
id += nums[Math.floor(Math.random() * nums.length) + 1]
}
return id
}
And here's more functional and readable code:
const makeid = () => {
let id = '';
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const nums = '0123456789'
id += generate(2, chars)
id += generate(3, nums)
return id
}
const generate = (number, string) => {
let res = ''
for (let i = 0; i < number; i++) {
res += string[Math.floor(Math.random() * string.length) + 1]
}
return res
}
console.log(makeid())