Skip to content

Instantly share code, notes, and snippets.

@jinwolf
Last active December 5, 2015 07:02
Show Gist options
  • Select an option

  • Save jinwolf/17a8294202aac00b68c1 to your computer and use it in GitHub Desktop.

Select an option

Save jinwolf/17a8294202aac00b68c1 to your computer and use it in GitHub Desktop.
JavaScript permutation
// a + permute(b, c, d);
// a + b + permute(c, d);
// a + c + permute(b, d);
// a + d + permute(b, c)
function permute(str) {
var used = new Array(4);
var list = str.split('');
var out = [];
doPermute(used, list, 0, out);
}
function doPermute(used, list, start, out) {
if (start == list.length) {
return console.log(out.join(''));
}
for (var i=0; i < list.length; i++) {
if (used[i] === true) {
continue;
}
used[i] = true;
out.push(list[i]);
doPermute(used, list, start + 1, out);
used[i] = false;
out.pop();
}
}
//permute(list);
permute('abcd');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment