JSFiddle - React, Tailwind, and code Playground

JavaScript

function pad(input) {
    var BASE = "0000";
    return input ? BASE .substr(0, 4 - Math.ceil(input / 10)) + input : BASE;
}


alert(pad(0));
alert(pad(9999));