Loop control in Javascript Programs