Now with the actual Javascript code!

I originally wrote this in Ruby, but now it's in Javascript.