0

Say I have [10, 100], n = 3. (e.g. like we have 3 digits for a number) I need to generate all possibles in javascript.

I think the output will be

[
[10, 10, 10],
[10, 10, 100],
[10, 100, 10],
.......
]
kenpeter
  • 7,404
  • 14
  • 64
  • 95
  • Have you tried anything yet? – Nathan May 13 '18 at 02:58
  • @Nathan, I know how to do permutation, but unsure n position. https://stackoverflow.com/questions/9960908/permutations-in-javascript – kenpeter May 13 '18 at 03:01
  • If you have your permutation, you can "push" it. E.g. your next permutation is probably var v = [10, 100, 100];, so arrayx.push(v); – dcromley May 13 '18 at 03:09
  • @dcromley https://stackoverflow.com/questions/30340361/permutations-with-repetition-using-recursion-javascript – kenpeter May 13 '18 at 03:18

1 Answers1

1

Permutations with repetition using recursion - JavaScript

I should do more search....

var abc = function () {
  var holdingArr = [];
  var threeOptions = ["a", "b", "c"];
  var recursiveABC = function(singleSolution) {
      if (singleSolution.length > 2) {
        holdingArr.push(singleSolution);
        return;
      }
      for (var i=0; i < threeOptions.length; i++) {
        recursiveABC(singleSolution.concat([threeOptions[i]]));
      }
  };
  recursiveABC([]);
  return holdingArr;
};
kenpeter
  • 7,404
  • 14
  • 64
  • 95