I'm writing my first javascript/html code to collect football results and then produce a league table. My code copes with single digit scores fine but not double digit scores.
Three arrays are used for results, league and players as below:
Results=[];
var Results=[
["Home","F","A","Away"],
["Si",,,"Ste"],
["Si",,,"Gaz"],
["Ste",,,"Gaz"],
["Ste",,,"Si"],
["Gaz",,,"Si"],
["Gaz",,,"Ste"],
["Si",,,"Ste"],
["Si",,,"Gaz"],
["Ste",,,"Gaz"],
["Ste",,,"Si"],
["Gaz",,,"Si"],
["Gaz",,,"Ste"],
];
League=[];
var League=[
["Team","P","W","D","L","F","A","GD","Pts"],
["Si",,,,,,,,],
["Ste",,,,,,,,],
["Gaz",,,,,,,,]
];
players=[];
var players=["Si","Ste","Gaz"];
I believe the faulty code is where wins are calculated:
if (Results[i][1]>Results[i][2])
{ wins++;
pts= +pts + 3;
}
This is taken from two 'for' loops that iterate through the results array for each player and populate the league array accordingly:
for (p = 0; p < players.length; p++)
{
for (i = 1; i < Results.length; i++)
{
if (Results[i][1]!= "")
{
if (Results[i][0]==players[p])
{
pld++;
goalsF=+goalsF + +Results[i][1];
goalsA=+goalsA + +Results[i][2];
gd=(goalsF - goalsA);
if (Results[i][1]>Results[i][2])
{
wins++;
pts= +pts + 3;
}
else if (Results[i][1]<Results[i][2])
{
loses++;
}
else
{
draws++;
pts++
}
}
}
if (Results[i][1]!= "")
{
if (Results[i][3]==players[p])
{
pld++;
goalsF=+goalsF + +Results[i][2];
goalsA=+goalsA + +Results[i][1];
gd=(goalsF - goalsA);
if (Results[i][1]<Results[i][2])
{
wins++;
pts= +pts + 3;
}
else if (Results[i][1]>Results[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;
var pld=0;
var wins=0;
var draws=0;
var loses=0;
var goalsF=0;
var goalsA=0;
var gd=0;
var pts=0;
var r=0;
}
For a 10-1 score it works correctly:
For 10-2 (or 10-3) the result gets reversed and the player with the lower score is deemed the winner!?
It's as if the comparison is only working on the first digit of the scoreline. How do I fix this?