Skip to content

Instantly share code, notes, and snippets.

@kevinsimper
Last active January 28, 2018 14:55
Show Gist options
  • Select an option

  • Save kevinsimper/c2ec0a5099932db9e230e236f6da405b to your computer and use it in GitHub Desktop.

Select an option

Save kevinsimper/c2ec0a5099932db9e230e236f6da405b to your computer and use it in GitHub Desktop.
How to generate a list of all combinations with Javascript Generators
const letters = 'abcdefghijklmnopqrstuvxyzæøå'
const numbers = '0123456789'
const list = (letters + numbers).split('')
exports.generate = function* generate(length) {
function* gen(length) {
for(var i = 0; i < list.length; i++) {
if(length > 1) {
for (const val of gen(length - 1)) {
yield list[i] + val
}
} else {
yield list[i]
}
}
}
for(var i = 1; i <= length; i++) {
for (const val of gen(i)) {
yield val
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment