Make sure you are resetting your counter at the start of this function and setting your counter within this loop (or before, from the length property).