working through some algorithm problems using javascript