Problems with adding a limited number of objects to an array Javascript -
i designing program take list of names , store them 3 different arrays, add names single array in order split them 2 different teams (two other arrays) have managed far add final 2 arrays have nested if supposed stop values being added array after reaches length isn't working. can't figure out why if..else function isn't working , wondering if there out there similar problem when adding array using array.length have included jsfiddle because there quite lot of code involved , must credit
http://dreaminginjavascript.wordpress.com/2008/08/22/eliminating-duplicates/
part of code came in useful adding single records array.
http://jsfiddle.net/29q50yz0/
function randomfunction() { document.getelementbyid("test4").innerhtml = team1.length; if (team1.length <= 6 ) { //|| team2.length <= 5 (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); } } (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); } } (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, though there more 12 records looking 2 'teams' of 6 , looking code add remaining records team2 when team1 reaches length of six, have included test outputs show array.length working should there problem within js.
many in advance
that functions bit of mess. have @ approach:
- put 3 arrays same array, behave in same way
- for each array (tier) same thing players
if team not full, check if random number right one, or other team 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 not full or team2 full team1.push(allplayers[ti][pi]); } else if( team2.length < 6 && (randomnumber1 > 4 || team1.length == 6)) { //team2 not full or team1 full team2.push(allplayers[ti][pi]); } } } //i don't know bit doing :d document.getelementbyid("test2").innerhtml = team1; document.getelementbyid("test3").innerhtml = team2; document.getelementbyid("team1").innerhtml = out1; }
Comments
Post a Comment