Last active
December 2, 2016 03:40
-
-
Save Trshant/d7365163b096aefd8f63b927c3d72411 to your computer and use it in GitHub Desktop.
Getting combinations from an array
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
function combinations(str) { | |
var fn = function(active, rest, a) { | |
if (!active && !rest) | |
return; | |
if (!rest) { | |
a.push(active); | |
} else { | |
fn(active + rest[0], rest.slice(1), a); | |
fn(active, rest.slice(1), a); | |
} | |
return a; | |
} | |
return fn("", str, []); | |
} |
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
combinations("abcd") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment