Brian's company needs to track financial information indexed by 100 digit routing numbers. Now, obviously, not all of those digits are significant, so if a user enters "123", the application needs to be smart enough to pad out the other 97 digits with leading zeros. Sane people might think this should be implemented as a one-line call to a built-in method. The more DIY among us might waste time building up a for loop. And, of course, a LISP fan would simply torture future coders with recursion and parentheses.
Then there's this approach that Brian found:
function ZeroFill(Data) {
var length = Data.length;
var NewData;
if (length >= 100) {
NewData = Data.substring(0,length);
}
if (length == 99) {
NewData = "0" + Data.substring(length-99,length);
}
if (length == 98) {
NewData = "00" + Data.substring(length-98,length);
}
if (length == 97) {
NewData = "000" + Data.substring(length-97,length);
}
/*
...
SNIPPED FOR REALLY LONG AND REPETITIVE CODE
...
*/
if (length == 2) {
NewData = "000000000000000000000000" + "00000000000000000" +
"0000000000000000" + "0000000000000000" +
"0000000000000000000000000" + Data.substring(length-2,length);
}
if (length == 1) {
NewData = "0000000000000000000000000" + "00000000000000000" +
"0000000000000000" + "0000000000000000" +
"0000000000000000000000000" + Data.substring(length-1,length);
}
return NewData;
}