refine code, use array to define js functions