refactor code javascript code.