js using for and foreach loop