Solution 3 in JS
Solution 4 in JS
Solution 5 in Ruby