Advertisement
stanevplamen

Combinations JS

Sep 26th, 2022
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.34 KB | Software | 0 0
  1.  
  2. var numbersAmount = 5;
  3. console.log("Please enter the amount of the numbers 1...N, N = " + numbersAmount);
  4. var combinationsAmount = 3;
  5. console.log("Please enter the amount of the combinations K = " + combinationsAmount);
  6. var vector = new Array(combinationsAmount);
  7. debugger;
  8. combinations(combinationsAmount - 1, vector, numbersAmount, 1);
  9.  
  10. function combinations(index, vector, numbersLength, start) {
  11.     if (index == -1) {
  12.         console.log(vector);
  13.     }
  14.     else {
  15.         for (let i = start; i <= numbersLength; i++) {
  16.             vector[index] = i;
  17.             combinations(index - 1, vector, (numbersLength), (i + 1));
  18.         }
  19.     }
  20. }
  21.  
  22.  
  23. var arr = "abcde";
  24. var numbersAmount = arr.length;
  25. console.log("Please enter the amount of the numbers 1...N, N = " + numbersAmount);
  26. var combinationsAmount = 3;
  27. console.log("Please enter the amount of the combinations K = " + combinationsAmount);
  28. var vector = new Array(combinationsAmount);
  29. debugger;
  30. combinations(combinationsAmount - 1, vector, numbersAmount, 1);
  31.  
  32. function combinations(index, vector, numbersLength, start) {
  33.     if (index == -1) {
  34.         console.log(vector);
  35.     }
  36.     else {
  37.         for (let i = start; i <= numbersLength; i++) {
  38.             vector[index] = arr[i-1];
  39.             combinations(index - 1, vector, (numbersLength), (i + 1));
  40.         }
  41.     }
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement