Created
October 4, 2015 00:38
-
-
Save claushellsing/479f4919ff387841c592 to your computer and use it in GitHub Desktop.
Create all the N combinations of a characters
This file contains hidden or 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
/** | |
* 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