I am creating a method that creates all possible combinations of a given series and stores them in an array (i.e. all the possible combinations to a bicycle combo lock). The way I am going about doing this is to first randomly create a series, then check to see if that series is already stored in the master list. If it is then it is tossed, if it is not then it is pushed into the array. Here is the code (working progress):
function constructArrays(hits:int,misses:int):void {
var tempArrayLength:int=hits+misses;
var arrayLength:int=combination(tempArrayLength,numberOfOutcomes);
for (var j=0; j<arrayLength; j) {
var win:int=0;
var loss:int=0;
for (var i=0; i<tempArrayLength; i++) {
var rando:int=Math.floor(Math.random()*2);
if (rando==1&&win<hits) {
testSequence*=rando;
win++;
} else if (rando==0&&loss<misses) {
testSequence*=rando;
loss++;
} else if (rando==1&&win==hits) {
testSequence*=0;
loss++;
} else if (rando==0&&loss==misses) {
testSequence*=1;
win++;
}
}
if (sequences.some(isDuplicate)==true) {
j++;
testSequence=new Array();
} else {
sequences.push(testSequence);
testSequence=new Array();
j++;
}
}
}
function isDuplicate(item:*, index:int, array:Array):Boolean {
return (item==testSequence);
}
The problem I am having is that the outside for loop (the first one, with var j…) seems to be iterating while the array.some() is still looping through the array, meaning it is matching different series against different indexes of the master list. My understanding of loops is that they don’t iterate before all the code is finished executing, is this not the case when something like the some() method is nested inside it? This seems to me like it must be glaringly obvious mistake I’m making, so I appreciate a second set of eyes.
Edit: I found an addition problem I don’t understand. In the conditional where it either pushes or does not push the series into the array, traces are telling me that the second conditional (which pushes the series into the array) is executing before the array.some() function is. Is there any way to explain how a conditional statement is being executed before the condition itself is checked? Or is it normal for traces from the array.some() function to show up after the trace from the conditional?
In addition, my gut is telling me that there is an easier way to go about doing this project, so if anyone has creative ideas or experience with something like this, it is also appreciated.