parse int into 2 digits format javascript

Solutions on MaxInterview for parse int into 2 digits format javascript by the best coders in the world

showing results for - "parse int into 2 digits format javascript"
Maja
13 Jan 2018
1function leftPad(number, targetLength) {
2    var output = number + '';
3    while (output.length < targetLength) {
4        output = '0' + output;
5    }
6    return output;
7}
8// Output :- 
9// leftPad(1, 2) // 01
10// leftPad(10, 2) // 10
11// leftPad(100, 2) // 100
12// leftPad(1, 3) // 001
13// leftPad(1, 8) // 00000001