tags:

views:

56

answers:

2

I am in need of a javascript function which can take a value and pad it to a given length (I need spaces, but anything would do). I found this:

http://jsfromhell.com/string/pad

But I have no idea what the heck it is doing and it doesn't seem to work for me.

+3  A: 

http://www.webtoolkit.info/javascript-pad.html

It's a lot more readable.

David Stratton
No doubt, Thanks a lot!
Anthony Potts
+2  A: 

The key trick in both those solutions is to create an array instance with a given size (one more than the desired length), and then to immediately call the "join()" method to make a string. The "join()" method is passed the padding string (spaces probably). Since the array is empty, the empty cells will be rendered as empty strings during the process of joining the array into one result string, and only the padding will remain. It's a really nice technique.

Pointy