Reputation: 285
Ok so I have 2 arrays, one consisting of 20 teams and one consiting of the league fixtures. I then have a very large for loop which loops through each single fixture and allocates the points, goals scored, goals allowed etc to each team. Everything works so long as there isn't more than 20 fixtures in the fixture array. Once I put the 21st fixture in the array my table crashes and I'm not sure why.
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;
}
}
}
}
And so on and so on until it loops through each team
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;
};
Upvotes: 0
Views: 70
Reputation: 1
You have a few places where you do something like
teams[j].GD = teams[i].GF - teams[i].GA;
Now, i
is index of fixtures
, and j
is index of teams
as you have 20 teams and 21 fixtures, when j == 20 (the 21st index) teams[i] will be UNDEFINED
Upvotes: 1