Skip to content

Instantly share code, notes, and snippets.

@itacirgabral
Last active January 9, 2019 15:00
Show Gist options
  • Save itacirgabral/656dee477dd9b68f83779cdffdcb7555 to your computer and use it in GitHub Desktop.
Save itacirgabral/656dee477dd9b68f83779cdffdcb7555 to your computer and use it in GitHub Desktop.
var vogal = ['a', 'e', 'i', 'o', 'u']
var impar = ['1', '3', '5', '7', '9']
var par = ['0', '2', '4', '6', '8']
var range = [par, vogal, impar]
function* combi(range) {
const total = range.reduce((a, b) => a * b.length, 1) - 1
let idx = range.map(arr => 0)
lasti = idx.length - 1
yield range.map((e, i) => e[idx[i]])
for (let _n = 0; _n < total; _n++) {
idx[lasti] = idx[lasti] + 1
range.reduceRight((a, b, i) => {
if (a[i] === b.length) {
a[i] = 0
a[i - 1] = a[i - 1] + 1
}
return a
}, idx)
yield range.map((e, i) => e[idx[i]])
}
}
for (const c of combi(range)) {
console.log(c.join(''))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment