Skip to content

Instantly share code, notes, and snippets.

@bennycode
Created March 20, 2017 13:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennycode/518cd87c4539d157b5576ff4b99f4765 to your computer and use it in GitHub Desktop.
Save bennycode/518cd87c4539d157b5576ff4b99f4765 to your computer and use it in GitHub Desktop.
Generate all possible words with a given set of characters
function allPossibleCombinations(input, fixedLength, currentCombination) {
if (currentCombination.length == fixedLength) {
return [currentCombination];
}
const combinations = [];
for (let i = 0; i < input.length; i++) {
combinations.push(...allPossibleCombinations(input, fixedLength, currentCombination + input[i]));
}
return combinations;
}
const input = ['a', '1'];
console.log(allPossibleCombinations(input, 2, '')); // ['aa', 'a1', '1a', '11']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment