1let string = ‘Alice’;
2// padStart() — we assume our string needs to have 10 characters
3string.padStart(10, ‘o’); // returns ‘oooooAlice’
4// padEnd()
5string.padEnd(10, ‘o’); // returns ‘Aliceooooo’;
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