Controlling the tiles creation in JavaScript