Skip to content

Instantly share code, notes, and snippets.

@bennycode
Created March 20, 2017 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennycode/36bfcdaa39358d3f1de366e360d2133f to your computer and use it in GitHub Desktop.
Save bennycode/36bfcdaa39358d3f1de366e360d2133f to your computer and use it in GitHub Desktop.
Permutation in JavaScript
const Combinatorics = require('js-combinatorics');
const n = ['A', 'B', 'C', 'D'];
const k = 2;
const generator = Combinatorics.bigCombination(n, k);
const combinations = generator.toArray().length;
console.log(`Possible combinations: ${combinations}`); // "Possible combinations: 6"
let combination = undefined;
while (combination = generator.next()) {
console.log(combination);
}
/**
* [ 'A', 'B' ]
* [ 'A', 'C' ]
* [ 'A', 'D' ]
* [ 'B', 'C' ]
* [ 'B', 'D' ]
* [ 'C', 'D' ]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment