I'm working on a recursive algorithm that takes in an array with three different elements (say ['a', 'b', 'c']
and returns a two-dimensional array with all the possible variations with repetition allowed (so [['a', 'a', 'a'], ['a', 'a', 'b'], ['a', 'b', 'b'],...]
). However my code fails and I'm not sure why.
var abc = function () {
var holdingArr = [];
var threeOptions = ["a", "b", "c"];
var singleSolution = [];
var recursiveABC = function(arr) {
if (singleSolution.length > 2) {
holdingArr.push(singleSolution);
singleSolution = [];
return;
}
for (var i=0; i < arr.length; i++) {
recursiveABC(arr.slice(i+1));
}
};
recursiveABC(threeOptions);
return holdingArr;
};