Solution 7 in Ruby and JS