0

Now, I want to set the output generated in constant( first two in letters,last three in numeric). What code should I use? Thanks.

Javascript

function makeid() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 5; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}
console.log(makeid());

6 Answers 6

3

You could try separating your 'possible' string into possibleNums and possibleChars

var possibleNums = "0123456789";
var possibleAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

for (var i = 0; i < 2; i++) {
    text += possibleAlpha.charAt(Math.floor(Math.random() * possibleAlpha.length));
}

for (var i = 0; i < 3; i++) {
    text += possibleNums.charAt(Math.floor(Math.random() * possibleNums.length));
}
Sign up to request clarification or add additional context in comments.

1 Comment

was just about to post the same solution
1

Try this:

function makeid() {
  var text = "";
  var possibleLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  var possibleNumbers = "0123456789";

  for (var i = 0; i < 2; i++)
    text += possibleLetters.charAt(Math.floor(Math.random() * possibleLetters.length));
  for (var i = 0; i < 3; i++)
    text += possibleNumbers.charAt(Math.floor(Math.random() * possibleNumbers.length));

  return text;
}

Comments

1

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())

Comments

0

Actually you don't need the string "possible" you can use ascii values.

below is the code.

function makeid() {
  var text = "";
var x,y;
  for (var i = 0; i < 2; i++){
  x=(Math.floor(Math.random()*2))?( Math.floor(Math.random() * (26)) + 65):( Math.floor(Math.random() * (26)) + 97)
  text+=String.fromCharCode(x)
  }
  for (var i = 0; i < 3; i++){
 x=String.fromCharCode(Math.floor(Math.random() * (10)) + 48)
  text+=x
  } 
  return text;
}
console.log(makeid()); 

Comments

0

var randomBetween = (min,max)=> Math.floor(max-Math.random()*(max-min));
var randomLetter = ()=> String.fromCharCode(randomBetween(65,90)+(Math.round(Math.random())?32:0));
var makeId = pattern=> (pattern||'xx000').replace(/./g, v=>isNaN(v)?randomLetter():randomBetween(0,9));

console.log(makeId());

Or you can also make your own patterns for example:

makeId('aaaaaa11111');   //returns: lUmKUe07356
makeId('a1a1a1a1a1a1a'); //returns: b2N5U8J2m7r2C

Any matching letter will be replaced with a random one, any number with a random number.

Comments

0

You can try the following:

function getRandomId() {
    return String.fromCharCode((Math.random() < 0.5 ? 65 : 97) + Math.floor(Math.random() * 26)) +
        String.fromCharCode((Math.random() < 0.5 ? 65 : 97) + Math.floor(Math.random() * 26)) +
        Math.random().toString(10).substring(2, 5);
}

for (var i = 0; i < 5; i++) {
    console.log(getRandomId());
}

/* Sample Output:
wc257
bR495
Li690
cF973
kf790
*/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.