I’m attempting to create the sample the place the quantity will auto-increment with at the least one prefix with zero when the quantity is over. for instance preliminary worth will likely be ABCD0001
once I add 1 it is going to be ABCD0002
so on it’ll repeatedly work like ABCD9999
. now the digit is over then it’ll convert to ABCD010000
means if the digit will over auto the quantity zero add as like right here we will see ABCD010000
I’m attempting to create this perform nevertheless it removes all of the zeroes from the code. please assist how I create this perform which might give the anticipated output within the javascript. I’m attempting to create nevertheless it not working as beneath.
It’s a little bit inconsistent that you’ve ABCD0001, ABCD0002 after which go to ABCD10000, ABCD10001. So should you enhance the variety of digits, you need to begin with ABCD1, ABCD2 and so forth.
However here’s a resolution:
let id = "ABCD9991";
for(let i = 0; i < 100; i++)
let strings = id.exchange(/[0-9]/g, '');
let digits = (parseInt(id.exchange(/[^0-9]/g, '')) + 1).toString();
if(digits.size < 4)
digits = ("000"+digits) .substr(-4);
id = strings + digits;
console.log(id);
1 Like
Welcome to the discussion board @wawane7256
My try
First a perform to get the required padding size of a given string
// strings have a size property '22'.size === 2
perform getPadLength ( size )
if (size < 5) return 4
// if an odd quantity in size return the size + 1
return (size % 2 === 0) ? size : size + 1
getPadLength('24') // 4
getPadLength('52367') // 6
getPadLength('102367') // 6
Now a perform to pad with zeros
perform padZeros (num)
const numStrg = num.toString()
const padding = getPadLength(numStrg)
return numStrg.padStart(padding, '0')
padZeros(24) // '0024'
padZeros(10235) // '010235'
padZeros(102356) // '102356'
[2, 64, 512, 1024, 32768].map(padZeros)
// ['0002', '0064', '0512', '1024', '032768']
thanks in your reply…
perform padZeros (num, mynumber)
const numStrg = num.toString() + mynumber;
const padding = getPadLength(numStrg)
return numStrg.padStart(padding, '0')
console.log(padZeros(‘ABCD0001’, 1)); // 0ABCD00011
anticipated output: ABCD0002
very good, nevertheless it deprecated as NodeJs saying…
What i gave you simply offers with the numbers. I believed you may have the ability to determine the remainder
Possibly you could possibly break up the string first. Then be part of afterwards.
Sorry, this occurs should you swap to usually between PHP and JS
Use substring() as an alternative.
2 Likes