JavaScript code to implement zero padding:
The Code is as follows:
/* Quiet Method */
Function pad (num, n ){
Var I = (num + ""). length;
While (I ++ <n) num = "0" + num;
Return num;
}
The above code is too cool to reflect my real level. So there is implementation 2:
The Code is as follows:
/* Magic Recursion Method */
Function pad2 (num, n ){
If (num + ""). length> = n) return num;
Return pad2 ("0" + num, n );
}
The magic recursion allows mm to praise and admire each time ~~
However, the trick is a fatal one. The above Code uses two lines, which is an insult. Think about it, a line of Magic Code comes to mind:
The Code is as follows:
/* Odd sex technique */
Function pad3 (num, n ){
Return (Array (n). join (0) + num). slice (-n );
}
This time, mm sent a worship eye, hiahia ~~~
=== I am not a space separator, but a very long time splitter. After n years ====
One day, the autumn is refreshing and the heart is relaxed. Sit down in the bodhi tree with friends and play chess with wine.
You said: BP has hurt me since you left. The magic code that you left leaves my hair three years ahead of schedule.
I'm surprised: What should I do?
My friend looked sad: Do you remember the magical pad3 function? Within one year after you leave, the company's business surge. Pad3 has a bug. In some cases, the first few digits are truncated. For example, pad3 (123456, 5) returns 23456. This bug was discovered by hackers, resulting in several large-scale phishing events and the company's loss of millions. At that time, the boss ordered me to find out the bug in a day, and it took me three days to locate the bug because of your magic code. In addition to pad3, there is also a magic xxoo9 function, at that time ......
Old friends are still nagging. After hearing the first paragraph, I was lost. Looking down at the writing of pad3, there is indeed a bug, which is used in the transaction system, crazy sweat ......
After talking to friends, I couldn't wait to retrieve the pad3 and xxoo9 code from the face's ghost cloud. I was shocked by the test:
Tricky_code.html
Pad3 not only has a character truncation bug, but also has performance problems ......
A fierce ideological struggle.
In the dark, the monk smiled and laughed at the following:
The world laughed at me, and I could not see the world.
To solve this problem, it seems that simplicity and simplicity are real knowledge.
Tang Miao threw a scroll and went with a laugh:
The Code is as follows:
/* Simple persistent Storage Method */
Function pad (num, n ){
Var len = num. toString (). length;
While (len <n ){
Num = "0" + num;
Len ++;
}
Return num;
}
Watching, I also laughed, with the passing of the Emperor ......
Note: I always want to write such an article. Today, 51js hangs out and occasionally sees the problem that the number of digits is not enough to fill in the previous 0, so I immediately got the idea of this article. For zero completion, my odd behavior is return Array (n-("" + num ). length + 1 ). join (0) + num; (there are also bugs), similar to the fruit, but considering the adequacy (the fruit bug is more concealed), the final result code is used as an example. In any case, thanks to fruit.