fix javascript loop variable scope