refactor code by use single if and create listToArray