Skip to content

Instantly share code, notes, and snippets.

@Trshant
Last active December 2, 2016 03:40
Show Gist options
  • Save Trshant/d7365163b096aefd8f63b927c3d72411 to your computer and use it in GitHub Desktop.
Save Trshant/d7365163b096aefd8f63b927c3d72411 to your computer and use it in GitHub Desktop.
Getting combinations from an array
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, []);
}
combinations("abcd")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment