(str,length,ch)
| 22 | // '00000000' 1 |
| 23 | |
| 24 | function leftpad2(str,length,ch){ |
| 25 | let len = length-str.length |
| 26 | total = '' |
| 27 | while(true){ |
| 28 | // if(len%2==1){ |
| 29 | if(len & 1){ |
| 30 | total+=ch |
| 31 | } |
| 32 | if(len==1){ |
| 33 | return total+str |
| 34 | } |
| 35 | ch += ch |
| 36 | len = len >> 1 |
| 37 | // len = parseInt(len/2) |
| 38 | } |
| 39 | |
| 40 | } |
| 41 | console.log(leftpad2('hello',10,'0')) |
| 42 | |
| 43 |