I am currently designing a program that will take a list of names and store them into 3 different arrays, it will then add all the names into another single array in order to split them into two different teams (two other arrays) I have managed so far to get everything to add into the final two arrays properly but I have a nested IF which is supposed to stop values being added into the array after it reaches a certain length that isn't working. I can't figure out why this IF..Else function isn't working and was just wondering if there was anyone out there with a similar problem when adding to an array using array.length
I have included a jsFiddle because there is quite a lot of code involved and I also must credit
http://dreaminginjavascript.wordpress.com/2008/08/22/eliminating-duplicates/
for their part of code which came in useful for only adding single records into the array.
http://jsfiddle.net/29q50yz0/
function randomFunction() {
document.getElementById("test4").innerHTML = team1.length;
if (team1.length <= 6 ) { //|| team2.length <= 5
for (index = 0; index < tier1.length; index++) {
randomNumber1 = Math.floor(Math.random() * 9);
if (randomNumber1 <= 4) {
team1.push(tier1[0]);
eliminateDuplicates(team1);
tier1.splice(0, 1);
} else {
team2.push(tier1[0]);
eliminateDuplicates2(team2);
tier1.splice(0, 1);
}
}
for (index = 0; index < tier2.length; index++) {
randomNumber1 = Math.floor(Math.random() * 9);
if (randomNumber1 <= 4) {
team1.push(tier2[0]);
eliminateDuplicates(team1);
tier2.splice(0, 1);
} else {
team2.push(tier2[0]);
eliminateDuplicates2(team2);
tier2.splice(0, 1);
}
}
for (index = 0; index < tier3.length; index++) {
randomNumber1 = Math.floor(Math.random() * 9);
if (randomNumber1 <= 4) {
team1.push(tier3[0]);
eliminateDuplicates(team1);
tier3.splice(0, 1);
} else {
team2.push(tier3[0]);
eliminateDuplicates2(team2);
tier3.splice(0, 1);
}
}
} else if (team1.length > 6) {
team2.concat(tier1, tier2, tier3);
document.getElementById("test2").innerHTML = team1;
document.getElementById("test3").innerHTML = team2;
} else if (team2.length > 6) {
team1.concat(tier1, tier2, tier3);
document.getElementById("test2").innerHTML = team1;
document.getElementById("test3").innerHTML = team2;
}
document.getElementById("test5").innerHTML = team1.length; }
please note, even though there are more than 12 records I am looking for two 'teams' of six and I am looking for the code to add the remaining records into team2 when team1 reaches a length of six, I have included some test outputs to show that the array.length is working as it should but there is a problem within the JS.
Many thanks in advance