Solved problem 35 in Ruby and JS