好的,所以我有2个阵列,一个由20支球队组成,另外一支由联盟赛程组成。然后,我有一个非常大的for循环,它循环遍历每个单一的夹具,并为每个团队分配得分,目标得分,目标允许等。只要夹具阵列中没有超过20个夹具,一切都能正常工作。一旦我将第21个灯具放入阵列中,我的桌子就崩溃了,我不知道为什么。
var teams = [
{id: 1, name: "AC Milan", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 2, name: "AS Roma", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 3, name: "Atalanta", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 4, name: "Bologna", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 5, name: "Benevento", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 6, name: "Cagliari", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 7, name: "Chievo", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 8, name: "Crotone", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 9, name: "Fiorentina", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 10, name: "Genoa", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 11, name: "Hellas Verona", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 12, name: "Inter", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 13, name: "Juventus", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 14, name: "Lazio", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 15, name: "Napoli", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 16, name: "Sampdoria", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 17, name: "Sassuolo", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 18, name: "SPAL", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 19, name: "Torino", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
{id: 20, name: "Udinese", GP:0, W: 0, D: 0, L:0, GF:0, GA:0, pts:0},
];
var fixtures = [
//matchday1
{match: 1, Homeid: 13, Awayid: 6, Homescore: 3, Awayscore: 0},
{match: 1, Homeid: 11, Awayid: 15, Homescore: 1, Awayscore: 3},
{match: 1, Homeid: 3, Awayid: 2, Homescore: 0, Awayscore: 1},
{match: 1, Homeid: 14, Awayid: 18, Homescore: 0, Awayscore: 0},
{match: 1, Homeid: 4, Awayid: 19, Homescore: 1, Awayscore: 1},
{match: 1, Homeid: 12, Awayid:9, Homescore: 3, Awayscore: 0},
{match: 1, Homeid: 20, Awayid: 7, Homescore: 1, Awayscore: 2},
{match: 1, Homeid: 16, Awayid: 5, Homescore: 2, Awayscore: 1},
{match: 1, Homeid: 17, Awayid: 10, Homescore: 0, Awayscore: 0},
{match: 1, Homeid: 8, Awayid: 1, Homescore: 0, Awayscore:3 },
//matchday2
{match: 1, Homeid: 10, Awayid: 13, Homescore: 2, Awayscore: 4},
{match: 1, Homeid: 5, Awayid: 4, Homescore: 0, Awayscore: 1},
{match: 1, Homeid: 2, Awayid: 12, Homescore: 1, Awayscore: 3},
{match: 1, Homeid: 19, Awayid: 17, Homescore: 3, Awayscore: 0},
{match: 1, Homeid: 1, Awayid: 6, Homescore: 2, Awayscore: 1},
{match: 1, Homeid: 7, Awayid:14, Homescore: 1, Awayscore: 2},
{match: 1, Homeid: 9, Awayid: 16, Homescore: 1, Awayscore: 2},
{match: 1, Homeid: 8, Awayid: 11, Homescore: 0, Awayscore: 0},
{match: 1, Homeid: 15, Awayid: 3, Homescore: 3, Awayscore: 1},
{match: 1, Homeid: 18, Awayid: 20, Homescore: 3, Awayscore: 2}
];
for (var i = 0; i < fixtures.length;i++) //For loop which calculates the Home and Away results for each team
{
if (fixtures[i].Homeid == 1)
{
for (var j = 0; j <teams.length; j++)
{
if (teams[j].id == fixtures[i].Homeid)
{
teams[j].GP += fixtures[i].match;
teams[j].GF += fixtures[i].Homescore;
teams[j].GA += fixtures[i].Awayscore;
teams[j].GD = teams[i].GF - teams[i].GA;
if (fixtures[i].Homescore > fixtures[i].Awayscore){
teams[j].W += 1;
}
else if (fixtures[i].Homescore < fixtures[i].Awayscore)
{
teams[j].L += 1;
}
else if (fixtures[i].Homescore == fixtures[i].Awayscore){
teams[j].D += 1;
}
}
}
}
if (fixtures[i].Awayid == 1 )
{
for (var j = 0; j <teams.length; j++)
{
if (teams[j].id == fixtures[i].Awayid)
{
teams[j].GP += fixtures[i].match;
teams[j].GF += fixtures[i].Awayscore;
teams[j].GA += fixtures[i].Homescore;
teams[j].GD = teams[i].GF - teams[i].GA;
if (fixtures[i].Homescore < fixtures[i].Awayscore){
teams[j].W += 1;
}
else if (fixtures[i].Homescore > fixtures[i].Awayscore)
{
teams[j].L += 1;
}
else if (fixtures[i].Homescore == fixtures[i].Awayscore){
teams[j].D += 1;
}
}
}
}
if (fixtures[i].Homeid == 2)
{
for (var j = 0; j <teams.length; j++)
{
if (teams[j].id == fixtures[i].Homeid)
{
teams[j].GP += fixtures[i].match;
teams[j].GF += fixtures[i].Homescore;
teams[j].GA += fixtures[i].Awayscore;
teams[j].GD = teams[i].GF - teams[i].GA;
if (fixtures[i].Homescore > fixtures[i].Awayscore){
teams[j].W += 1;
}
else if (fixtures[i].Homescore < fixtures[i].Awayscore){
teams[j].L += 1;
}
else if (fixtures[i].Homescore == fixtures[i].Awayscore){
teams[j].D += 1;
}
}
}
}
if (fixtures[i].Awayid == 2 )
{
for (var j = 0; j <teams.length; j++)
{
if (teams[j].id == fixtures[i].Awayid)
{
teams[j].GP += fixtures[i].match;
teams[j].GF += fixtures[i].Awayscore;
teams[j].GA += fixtures[i].Homescore;
teams[j].GD = teams[i].GF - teams[i].GA;
if (fixtures[i].Homescore < fixtures[i].Awayscore){
teams[j].W += 1;
}
else if (fixtures[i].Homescore > fixtures[i].Awayscore){
teams[j].L += 1;
}
else if (fixtures[i].Homescore == fixtures[i].Awayscore){
teams[j].D += 1;
}
}
}
}
依此类推,直到它遍历每个团队
for (var x = 0; x < teams.length; x++) //Calculates Total
points for all teams
{
teams[x].pts = teams[x].W * 3 + teams[x].D;
}
window.onload = function()
{
var tableContainer = document.querySelector("#table");
var table = "";
teams.sort(function(a,b)
{return b.pts - a.pts});
for (var i = 0; i<teams.length; i++)
{
table += "<tr>" +
"<td>" + teams[i].name + "</td>"+
"<td>" + teams[i].GP + "</td>"+
"<td>" + teams[i].W + "</td>"+
"<td>" + teams[i].D + "</td>"+
"<td>" + teams[i].L + "</td>"+
"<td>" + teams[i].GF + "</td>"+
"<td>" + teams[i].GA + "</td>"+
"<td>" + (teams[i].GF - teams[i].GA) + "</td>"+
"<td>" + ( (teams[i].W * 3) + teams[i].D) + "</td>"+
"</tr>";
}
tableContainer.innerHTML+=table;
};
答案 0 :(得分:1)
您可以在某些地方执行类似
的操作teams[j].GD = teams[i].GF - teams[i].GA;
现在,i
是fixtures
的索引,而j
是teams
的索引
因为你有20支球队和21支球队,当j == 20(第21指数)球队[i]将被定义