Skip to content

Instantly share code, notes, and snippets.

@claushellsing
Created October 4, 2015 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claushellsing/479f4919ff387841c592 to your computer and use it in GitHub Desktop.
Save claushellsing/479f4919ff387841c592 to your computer and use it in GitHub Desktop.
Create all the N combinations of a characters
/**
* Created by Usuario on 03/10/2015.
*/
var available_characters = ["a","b"];
console.log(combinations(available_characters,5));
function combinations(available_characters,count_char)
{
var acum_results = {combs : []};
available_characters.forEach(function (el,i,array) {
next_comb(el,available_characters,count_char,acum_results);
});
return acum_results.combs;
}
function next_comb(string,available_characters,count_char,acum_results)
{
if (string.length == count_char)
{
return 0;
}
available_characters.forEach(function (el,i,array) {
var str = string+""+el;
acum_results.combs.push(str);
next_comb(str,available_characters,count_char,acum_results);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment