简体   繁体   English

对于循环退出之前,我希望它

[英]For loop exiting before I want it to

Ok so I have 2 arrays, one consisting of 20 teams and one consiting of the league fixtures. 好的,所以我有2个阵列,一个由20支球队组成,一个由联赛装备组成。 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. 然后,我有一个非常大的for循环,该循环遍历每个固定装置,并为每个团队分配分数,得分,允许的目标等。 Everything works so long as there isn't more than 20 fixtures in the fixture array. 只要夹具阵列中的夹具不超过20个,一切就可以正常工作。 Once I put the 21st fixture in the array my table crashes and I'm not sure why. 一旦将第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;

                }
                }
        }
    }   

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;
};

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 现在, ifixtures索引,而jteams索引

as you have 20 teams and 21 fixtures, when j == 20 (the 21st index) teams[i] will be UNDEFINED 因为您有20支球队和21支固定装置,所以当j == 20(第21个索引)时,球队[i]将是未定义的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM