solved problem 9 in JS and Ruby