replaced for loops with indexOf method