Last active
July 18, 2019 15:01
-
-
Save figloalds/0317622e9f331046738f5da5154145b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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