Breaking Text into Chunks
Another simple modification to the code and we can set up a function (or better yet a new String method) that allows you to pass the size of the chunks you want the text broken into. Of course if we are going to do that then we really ought to also define a default size for the chunks in case we call the method and forget to tell it a size.
So how much code do we need to do all this? Not very much at all as you can see.
if (typeof n=='undefined') n=2;
var a = s.chunk(6);
This gives us our array with five elements each containing six characters from the string and is equivalent to specifying:
Of course if we wanted it broken into 15 chunks of two characters each we just need to remove the 6 from the second line and call s.chunk() and since 2 is the default size it will use that as the size for the chunks since we have not specified a different size. With the 6 removed our code is now equicalent to: