0

I've modified my first javascript program so that the football fixtures array is built based on a 'rounds' variable (the number of times teams play each other) rather than hardcoding it.

Although the fixtures appear to generate perfectly, it only produces a correct league table for 2 rounds. For 3 rounds or more, the code is unable to read results entered into the html table for rounds 1 and 2 but can from round 3 onwards?

Here's the code for generating the fixtures:

var players = ["P1", "P2", "P3"];
var rounds = 3;
var oddRound = [
  [players[0],,,players[1]],
    [players[0],,,players[2]],
    [players[1],,,players[2]]
  ];
var evenRound = [
  [players[1],,,players[0]],
    [players[2],,,players[0]],
    [players[2],,,players[1]]
  ];

var Fixtures = [];
Fixtures.length=0;
for (i = 1; i <= rounds; i++) {
  if (i & 1) {
     Array.prototype.push.apply(Fixtures, oddRound);
  } else {
     Array.prototype.push.apply(Fixtures, evenRound);
  }
}

And here's the code for calculating the league table:

// Update Fixtures with scores from html table

for (i = 0; i < Fixtures.length; i++) {

        Fixtures[i][1] = table.rows[i].cells[1].children[0].value;
        Fixtures[i][2] = table.rows[i].cells[2].children[0].value;
    }


    /*
     * Iterate through Fixtures
     * Update rows 1-7 of League i.e Pld,W,D,L,F,A,Pts
     */

    var pld = 0;
    var wins = 0;
    var draws = 0;
    var loses = 0;
    var goalsF = 0;
    var goalsA = 0;
    var gd = 0;
    var fpg = 0;
    var apg = 0;
    var pts = 0;
    var r = 0;

    var League = [
        ["Overall", "P", "W", "D", "L", "F", "A", "GD", "Pts"],
        [players[0], , , , , , , ],
        [players[1], , , , , , , ],
        [players[2], , , , , , , ]
    ];


    for (p = 0; p < players.length; p++) {
      for (i = 0; i < Fixtures.length; i++) {
        if (Fixtures[i][1] !== "") {
                if (Fixtures[i][0] == players[p]) {
                    pld++;
                    goalsF = +goalsF + +Fixtures[i][1];
                    goalsA = +goalsA + +Fixtures[i][2];
                    gd = (goalsF - goalsA);
                    fpg = goalsF / pld;
                    apg = goalsA / pld;
                    if (parseInt(Fixtures[i][1]) > parseInt(Fixtures[i][2])) {
                        wins++;
                        pts = +pts + 3;
                    } else if (parseInt(Fixtures[i][1]) < parseInt(Fixtures[i][2])) {
                        loses++;
                    } else {
                        draws++;
                        pts++;
                    }
                }
            }
            if (Fixtures[i][1] !== "") {
                if (Fixtures[i][3] == players[p]) {
                    pld++;
                    goalsF = +goalsF + +Fixtures[i][2];
                    goalsA = +goalsA + +Fixtures[i][1];
                    gd = (goalsF - goalsA);
                    fpg = goalsF / pld;
                    apg = goalsA / pld;
                    if (parseInt(Fixtures[i][1]) < parseInt(Fixtures[i][2])) {
                        wins++;
                        pts = +pts + 3;
                    } else if (parseInt(Fixtures[i][1]) > parseInt(Fixtures[i][2])) {
                        loses++;
                    } else {
                        draws++;
                        pts++;
                    }
                }
            }
        }
        r = p + 1;
        League[r][1] = pld;
        League[r][2] = wins;
        League[r][3] = draws;
        League[r][4] = loses;
        League[r][5] = goalsF;
        League[r][6] = goalsA;
        League[r][7] = gd;
        League[r][8] = pts;

        Attack[r][1] = pld;
        Attack[r][2] = goalsF;
        Attack[r][3] = fpg.toFixed(1);

        Defence[r][1] = pld;
        Defence[r][2] = goalsA;
        Defence[r][3] = apg.toFixed(1);

        var pld = 0;
        var wins = 0;
        var draws = 0;
        var loses = 0;
        var goalsF = 0;
        var goalsA = 0;
        var gd = 0;
        var fpg = 0;
        var apg = 0;
        var pts = 0;
        var r = 0;
    }

And here's the related HTML:

<html>
<body>

<h1>JFF 2020</h1>

<h4>v0.7.3a</h4>

<input type="button" value="Update" onclick="update()">

<div>
<table id="matches" border="2" style="float:left"> </table>
</div>

<div>
<table id="standings" border="2"> </table>
</div>

<div>
<table id="standings2" border="2"> </table>
</div>

<div>
<table id="standings3" border="2"> </table>
</div>

<script src="jff 0.7.3a.js"></script>

</body>
</html>

And finally here's the results I've been getting:

  • 2 rounds and all 6 fixtures are read perfectly

  • 3 rounds and first 3 fixtures (round#1) are 'invisible'?

  • 3 rounds and only 4th fixture (round#2) is read?

enter image description here

enter image description here

enter image description here

The code that produces the league table is unchanged. Any advice/guidance gratefully received :)

EDIT: Here's another aspect of this problem. You'll see below that entering just the 8th result produces an incorrect league. My code duplicates this result, adding it to Fixtures twice - once correctly for the 8th fixture but also incorrectly to the 3rd fixture which happens to be the same P2 v P3 fixture? (Contents of the array are listed as a string under the league table)

enter image description here

gdh48k
  • 29
  • 6
  • I don't see anything wrong with the code you posted (other than that `i` should be declared explicitly). It creates an array of arrays that looks exactly what the code appears to be intended to create. – Pointy Mar 22 '20 at 13:26
  • @Pointy I've just tried declaring `i` but same outcome unfortunately. Thanks for reply though. – gdh48k Mar 22 '20 at 13:30
  • Declaring `i` would not make a difference, it's just the right thing to do. No difference needs to be made anyway, as your code appears to work. I suspect your problems lie in the code that process that `Fixtures` array subsequently. – Pointy Mar 22 '20 at 13:36
  • 1
    what's up with the `Fixtures.length=0;`? also `(i = 1; i <= rounds; i++) {` should be `(let i = 1; i <= rounds; i++) {`. Also your if statement `if (i & 1) { ` you probably want `i % 2 === 0` – MonteCristo Mar 22 '20 at 13:37
  • @MonteCristo `i & 1` will (in this case) do exactly the same thing as `i % 2`. – Pointy Mar 22 '20 at 13:37
  • It may be your HTML? – MonteCristo Mar 22 '20 at 13:43
  • @Pointy Understand re `i`. Code added for processing of `Fixtures' – gdh48k Mar 22 '20 at 14:41
  • @MonteCristo My understanding is that `Fixtures.length=0;` clears the array. While debugging this issue, I became concerned that with every loop of the code, the Fixtures array was getting bigger and bigger, so this is an attempt to stop that. – gdh48k Mar 22 '20 at 14:47
  • @MonteCristo Thanks for suggestion re HTML but given it worked for 2 rounds, shouldn't it work for more? And what might I try? I'll add HTML to question now. – gdh48k Mar 22 '20 at 14:48
  • When you look at code you previously posted and just do console log you will notice fixtures seems to get created. Which means it could be something in your HTML for creating fixture table or your code in overall. you really need to stop declaring global vars e.g. `(i = 1; i <= rounds; i++)` It's a really bad thing. And if you look at the first for loop have `i` as your var and further down the line 3rd for loop you also have `i`. that could be your issue – MonteCristo Mar 22 '20 at 16:47
  • @MonteCristo Thanks again for your help. I'll re-work the arrays (players, fixtures and league) as global variables and loop counters (i, p and r) as local variables. I assume this involves converting my code into functions? I've also added a further edit in case it sheds more light on this issue. – gdh48k Mar 23 '20 at 15:47
  • no you just need to do and `let` before the `i` so that it's `(let i = 1; i <= rounds; i++)`. If you put just var it still make it available within entire file. you should also convert all your other `vars` to `let` or `const` difference between [let and var](https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var) . more on the [const](https://stackoverflow.com/questions/22308071/what-is-the-difference-between-let-and-const-ecmascript-2015-es6) – MonteCristo Mar 23 '20 at 16:59
  • @MonteCristo My grasp of scoping has improved, thanks for the links. And my code is looking in much better shape. My fixture problem is stubborn, however remains? Given hardcoded fixtures worked I can't help think it's something to do with how I'm now building the array, round by round? – gdh48k Mar 24 '20 at 15:19

0 Answers0