Created
March 20, 2017 10:17
-
-
Save bennycode/36bfcdaa39358d3f1de366e360d2133f to your computer and use it in GitHub Desktop.
Permutation in JavaScript
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
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