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?
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)