Q:

How Make Dynamic Zero Filled Number in Node js

so I was given the task to make a unique code by adding numbers after the code.
and I have tried and succeeded but there is one thing that blocks what if the number has reached its limit? 9999 value
how to reset it to 00001. Lift digit increased by 1
here's my snippet code
function getNewCode(value, callback){
    let newCode = _.upperCase(value)  + "-";
    let lastCode = newCode + "0001"

    Transaction.findOne({tr_number: new RegExp(newCode, 'i')}, (err, doc) => {

        if(err) callback(err)
        if (!_.isNil(doc)){
            let arr = doc.tr_number.split("-");

// in this part, i want got some conditional to set a new value 
//when it got max length = 9999 for an example

            let inc = parseInt(arr[1]) + 1;

            lastCode = newCode + ("0000"+inc).slice(-4);
            console.log('lastCode', ciduk, lastCode);

            return callback(lastCode);
        }else{
            return callback(lastCode);
        }
    }).sort({tr_number: -1})
};

sorry for my bad language :) grats.

A:

You can get number size by converting it toString() and get it length.
function getNext(prevStr) {
    let padSize = prevStr.length;
    let next = parseInt(prevStr, 10) + 1;
    if (next >= 10 ** padSize) {
        padSize++;
        next = 1;
    }
    return next.toString().padStart(padSize, '0');
}

console.log(getNext('0099')); // '0100'
console.log(getNext('9999')); // '00001'

