Home  >  Article  >  Web Front-end  >  JavaScript gap zero filling implementation code_javascript skills

JavaScript gap zero filling implementation code_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:33:541031browse

Implementation 1:

Copy code The code is as follows:

/* Bland method*/
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:
Copy code The code is as follows:

/* Magical recursion method* /
function pad2(num, n) {
if ((num "").length >= n) return num;
return pad2("0" num, n);
}

The magical recursion can make mm look at you with praise and admiration every time~~
However, when masters compete with each other, what matters is one fatal move. The above code actually takes two lines, which is simply insulting. After thinking about it, a line of magical code came to mind:
Copy code The code is as follows:

/ * Weird trick*/
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 dividing line, I am a long, long time dividing line. After n years ===
One day, the autumn air was crisp and my heart was relaxed and happy. Sitting under the bodhi tree with friends, drinking wine and playing chess.
Friend said: 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 look on his 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 how to write 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. After testing, I was shocked:
tricky_code.html
pad3 not only had character interception bugs, but also had performance issues...
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 my way.
When it comes to solving this confusion, simplicity is the true wisdom.
Tang Monk threw down a scroll and laughed away:
Copy code The code is as follows:

/* Simple long-lasting method*/
function pad(num, n) {
var len = num.toString().length;
while(len < n) {
num = "0" num;
len; 🎜>Note: I’ve always wanted to write an article like this. 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 The bug is more hidden), and finally I used Guoguo’s code as an example. Anyway, thanks Guoguo.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn