Home > Net >  Sequence auto generate function using JavaScript and node with mongo
Sequence auto generate function using JavaScript and node with mongo

Time:04-15

am new to JavaScript and I need to generate a sequence of 5 digit numbers with an alphabet being the 1st character (for ex: A00001). [totally it will be 6 letters with alphabet] and the sequence should go till A99999 and after that it should switch to alphabet B and start with B00000 and end with B99999 and this should go on till alphabet Z. Can anyone pls help me. Thanks in advance.

CodePudding user response:

var num = 0
var char = 'A'

while(true){
    console.log(char ('00000' num).slice(-5))
    num  = 1
    if(num>=100000){
        num=0
        char=String.fromCharCode(char.charCodeAt()   1)
        if(char.charCodeAt()>90)
            break
    }
    
}

If you want to create a new id using the previous id it can be done as done below:

var prev_id = 'A05786'

num = parseInt(prev_id.slice(1))

char = prev_id[0]

num  = 1
if(num>=100000){
    num=0
    char=String.fromCharCode(char.charCodeAt()   1)
}

new_id = char ('00000' num).slice(-5)
console.log(new_id) 
  • Related