jquery - JavaScript Objects and Arrays -
i'm having function adding 16 football teams highest number of points in 4 groups. every group must have 4 teams , must not have more 2 teams same league.
i've added property in every team object league name in arr
, sorted highest number of points.
i can't figure out how put condition 1 group can't have more 2 teams 1 league?
jsfiddle link: http://jsfiddle.net/6jdjjqfw/
code:
var tournaments = ['bundes', 'league1', 'premier', 'primera', 'seriea']; var teams = []; var groups = []; function getdata(id) { var url = "http://api.helloworld.ba/n/app.php?f=" + id; return $.getjson(url); } var ajax = []; (i = 0; < tournaments.length; i++) { ajax.push(getdata(tournaments[i])); } $.when.apply($, ajax).done(function () { (var = 0, len = arguments.length; < len; i++) { teams.push(arguments[i][0]); } creategroups(teams); }); function creategroups(teams) { var arr = [], arra = [], arrb = [], arrc = [], arrd = [], br = 1; (i = 0; < teams.length; i++) (var j = 0; j < teams[i].standing.length; j++) { teams[i].standing[j].league = teams[i].leaguecaption; arr.push(teams[i].standing[j]); } arr.sort(function (a, b) { return b.points - a.points; }); (i = 0; < 4; i++) { arra.push(arr[i].teamname); arrb.push(arr[i+4].teamname); arrc.push(arr[i+8].teamname); arrd.push(arr[i+12].teamname); } groups.push(arra); groups.push(arrb); groups.push(arrc); groups.push(arrd); console.log(teams); console.log(arr); console.log("groups listed:\n\n"); for(i=0; i<groups.length;i++) { console.log("group " + br +" " + groups[i]); br++; } }
try doing :
iterate on teams (16 items)in while loop
use switch on random int between 1 , 4 choose group of each team
function getrandomint(min, max) { return math.floor(math.random() * (max - min + 1)) + min; }
add method test if team has 2 member of league before adding group. test if group not full (4 members) if method returns false don't add team group , don't increase team counter, you'll new random number in loop.
Comments
Post a Comment