Skip to content

Instantly share code, notes, and snippets.

@alexislagante
Last active November 7, 2015 08:19
Show Gist options
  • Save alexislagante/e7025a7cd662908fb9a7 to your computer and use it in GitHub Desktop.
Save alexislagante/e7025a7cd662908fb9a7 to your computer and use it in GitHub Desktop.
Generate sequences of a string
function sequence(str, n) {
var chars = str.split("");
n = n || str.length;
var result = [""];
for (var i=0;i<n;i++) {
var newResult = [];
chars.forEach(function(char){
result.forEach(function(item) {
newResult.push(char+item);
});
});
result = newResult;
}
return result;
}
sequence("abcdef")
sequence("abcdefghij", 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment