Code:
function lzero(str, len)
{
str += ''; // cast to string
while(str.length < len)
{
str = "0".str;
}
return str;
}
When the value of str passed to the function is 0 to 9, I get the error "TypeError: str is undefined" for the while loop. Why is this?
I just want to add leading zeros to a number. Is there maybe a better way?