Javascript port of Pat Hooper's program