Refactor code to model eloquent javascripts style

Use a conditional while loop rather than series of if statements

Return a hash instead of array

Do tedious at end instead of in the middle

Use ternary operators for concise actions

Use slice instead of splice. Slice returns everything EXCEPT the
specified value. It does not mutate original string. Count starts from
1, not 0.