Solutions 5 and 6 in Ruby and JS