Fixing JS index in for loop