Implementation 1:
/* 平淡无奇法 */ function pad(num, n) { var i = (num + "").length; while(i++ < n) num = "0" + num; return num; }
The above code is too bland and does not reflect my true level. So there is implementation two:
/* 神奇递归法 */ function pad2(num, n) { if ((num + "").length >= n) return num; return pad2("0" + num, n); }
The magical recursion can make mm look at her with praise and admiration every time~~
However, when masters compete with each other, what matters is that one move is fatal. The above code actually uses two lines, which is simply insulting. After thinking about it, a line of magical code came to my mind:
/* 奇淫技巧法 */ function pad3(num, n) { return (Array(n).join(0) + num).slice(-n); }
This time mm gave me a look of admiration, hiahia~~~
=== I am not a space divider, I am very long A long dividing line of time. After n years ===
One day, the autumn air was crisp and I felt relaxed and happy. Sitting under the bodhi tree with friends, drinking wine and playing chess.
Friend said: Mr. BP, you have caused me so much pain since you left. Those magical codes you left caused my hair to fall out 3 years early.
I was shocked: How can I say this?
The friend looked back with a sad face: Do you remember that magical pad3 function? Within 1 year of your departure, business surged. There is a bug in pad3. In some cases, the first digits of the number will be intercepted. For example, pad3(123456, 5), returns 23456. This bug was discovered by hackers, which led to several large-scale phishing incidents and the company lost millions. At that time, my boss ordered me to find the bug within one day, but your magic code took me three days to locate it. In addition to pad3, there is also a magical xxoo9 function. At that time...
My old friend was still chattering, and when I finished listening to the first paragraph, I was lost. I lowered my head and thought about the writing method of pad3. There is indeed an interception bug. This is used in the trading system. I sweat...
After saying goodbye to my old friend, I couldn't wait to retrieve the codes of pad3 and xxoo9 from a cloud in GoogleFace. Test, shocked:
tricky_code.html
pad3 not only has character interception bug, but also has performance problems...
A fierce ideological struggle.
In the confusion, I saw Tang Monk coming from a distance, smiling and talking:
Everyone in the world laughs at me for being wordy, but the world can’t understand me.
When you want to solve this confusion, simplicity is the true knowledge.
Tang Monk threw down a scroll and laughed away:
/* 质朴长存法 */ function pad(num, n) { var len = num.toString().length; while(len < n) { num = "0" + num; len++; } return num; }
Looking at it, I also laughed and went with Tang Monk to learn the scriptures...
Note: I have always wanted to write such an article. I was wandering around 51js today and saw a post by chance: There is not enough digits to add 0 in front, so I immediately came up with the idea for this article. Regarding zero padding, my weird reaction is return Array(n - ("" + num).length + 1).join(0) + num; (there are also bugs), which is similar to Guoguo's, but considering the shock value (Guoguo's bug is more hidden). Finally, Guoguo's code is used as an example. Anyway, thanks Guoguo.
For more JavaScript gap zero filling implementation code related articles, please pay attention to the PHP Chinese website!