Skip to content

Instantly share code, notes, and snippets.

@figloalds
Last active July 18, 2019 15:01
Show Gist options
  • Save figloalds/0317622e9f331046738f5da5154145b6 to your computer and use it in GitHub Desktop.
Save figloalds/0317622e9f331046738f5da5154145b6 to your computer and use it in GitHub Desktop.
const getCombinations = (map, digitCount) => {
let numCombinations = Math.pow(map.length, digitCount);
const retv = [];
for(let i = 0; i < numCombinations; i++) {
let current = [];
let val = i;
do {
let idx = Math.floor(val % map.length);
current.push(map[idx]);
val = Math.floor(val / map.length);
} while ((val) > 0);
if(current.length < digitCount) {
current = current.concat(new Array(digitCount - current.length).fill(map[0]));
}
current = current.reverse();
retv.push(current.join(''));
}
return retv;
}
// use: getCombinations('1234', 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment