1const str1 = '5';
2
3console.log(str1.padStart(2, '0'));
4// expected output: "05"
5
6const fullNumber = '2034399002125581';
7const last4Digits = fullNumber.slice(-4);
8const maskedNumber = last4Digits.padStart(fullNumber.length, '*');
9
10console.log(maskedNumber);
11// expected output: "************5581"
12
1export function padStart(number, digits = 2, emptyDigit = 0) {
2 let length = 0;
3 let n = Math.abs(number);
4 let absoluteNumber = n;
5 do {
6 n /= 10;
7 length++;
8 } while (n >= 1);
9 const prefix = Array(Math.max((digits - length) + 1, 0)).join(emptyDigit);
10 return number < 0 ? `-${prefix}${absoluteNumber}` : prefix + number;
11}
12
13export default padStart;
14