1

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

1 Answer 1

2

That functions is a bit of a mess. Have a look at my approach:

  • Put all 3 arrays into the same array, as they behave in the same way
  • For each array (tier) do the same thing for all players
  • If the team is not full, check if the random number is the right one, or the other team is full

    function randomFunction() {
    
    var allPlayers = [tier1, tier2, tier3];
    for( var ti = 0; ti < allPlayers.length; ti++){ //foreach tier
        for( var pi = 0; pi < allPlayers[ti].length; pi++) { //foreach player
    
            randomNumber1 = Math.floor(Math.random() * 9);
    
            if( team1.length < 6 && (randomNumber1 <= 4 || team2.length == 6)) { //team1 is not full or team2 is full
                team1.push(allPlayers[ti][pi]);
            }
            else if( team2.length < 6 && (randomNumber1 > 4 || team1.length == 6)) { //team2 is not full or team1 is full
                team2.push(allPlayers[ti][pi]);
            }
        }
    
    }
    //I don't know what this bit is doing :D
    document.getElementById("test2").innerHTML = team1;
    document.getElementById("test3").innerHTML = team2;
    document.getElementById("team1").innerHTML = out1;
    }
    
Sign up to request clarification or add additional context in comments.

3 Comments

I will give it a try soon and i will let you know :) i appreciate the time and effort but sadly I don't have the rep to vote up, the bit at the bottom is for testing its more relevant on jdFiddle i will take them out of the code on here
If it works, just mark it as accepted. Please note that both teams are limited to 6 players, so there are a few from Tier3 that won't be assigned
I'm happy to say it works :) thanks a lot, yeah I plan on only having 12 records in it when I'm using it I just overloaded it for testing purposes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.