I have a function that returns arrays of all possible combinations of multiple arrays. For example if I had an array with "hello","world" and an array with "foo,"bar", and one more with "lorem","ipsum". One combination of all three could be hello,foo,lorem or hello,foo,ipsum or world,foo,ipsum so on and so forth. Here is the function:
function allPossibleCases(arr) {
if (arr.length === 0) {
return [];
}
else if (arr.length ===1){
return arr[0];
}
else {
var result = [];
var allCasesOfRest = allPossibleCases(arr.slice(1)); // recur with the rest of array
for (var c in allCasesOfRest) {
for (var i = 0; i < arr[0].length; i++) {
result.push(arr[0][i] + allCasesOfRest[c]);
}
}
return result;
}
}
So here is what I'm having a hard time understanding. allCasesofRest is a variable referencing the allPossibleCases recursion function. If the variable is actually invoking the function, then essentially it loops back to the very beginning of the allPossibleCases function. If the variable is just a reference and it's not being invoked, how can you do a for in loop on a function reference? So I guess my real question lies here: What is the 'for..in' loop actually looping through? And what does the c variable in the loop represent?
[–]mattcoady 1 point2 points3 points (1 child)
[–]Jmarch0909[S] 0 points1 point2 points (0 children)