Reputation: 45
I'm looking for insight into my if conditionals within my for loop. Currently, when I execute this function, the initial if conditional outside of the for loop runs, then the for loop starts. My test data has a length of 12, so the for loop runs the if (length > 10)
conditional twice, and then runs the else if (length == 10)
conditional. However, then it stalls and won't execute the next else if
conditional (else if (length > 3 && length <=10 && mergedGroup != [])
).
I think the issue lies with mergedGroup != []
but I'm not sure how to say "don't execute this conditional unless length > 3 and length <= 10 and mergedGroup array isn't empty."
Assume all functions and variables inside this executeFullRun
function are built and correct (I've tested them line by line in the console, just having issues putting them into one function to run all of them together).
Any insight?
function executeFullRun(players) {
if (group1 != [] && group2!= []) {
makeGroups(players);
}
var length = group1.length + group2.length;
for (i=0;i<length;i++) {
if (length > 10) {
while (group1.length + group2.length > 10) {
winTribalImmunity(group1,group2);
voteOffPreMerge(vulnerablePlayers);
console.log('Group Vote');
length -= 1;
console.log(length);
}
} else if (length == 10) {
if (group1.length + group2.length == 10) {
mergeGroups(group1,group2);
console.log('Merge Groups');
console.log(length);
}
} else if (length > 3 && length <=10 && mergedGroup != []) {
while (length > 3) {
winIndividualImmunity(mergedGroup);
voteOffPostMerge(vulnerableIndividuals);
console.log('Individual Vote');
length -= 1;
console.log(length);
}
} else {
if (length == 3) {
winGame(mergedGroup);
console.log('Winner!');
}
}
}
}
Upvotes: 0
Views: 66
Reputation: 171669
You can't compare an empty array to another empty array by doing someExistingArray != []
.
They are completely different object references and therefore are never equal, regardless if contents and length are identical.
Check the array length instead
Try
if (length > 3 && length <=10 && mergedGroup.length) {
And
if (group1.length && group2.length) {
Upvotes: 4