Use a common function for each method.

`function generate(pattern)`. It shouldn't be a `var` statement.