Last active
November 7, 2015 08:19
-
-
Save alexislagante/e7025a7cd662908fb9a7 to your computer and use it in GitHub Desktop.
Generate sequences of a string
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
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