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?


Results[i][1]andResults[i][2], but I guess they are strings, and strings do not compare in the same way as numbers. So either make sure you store number data types at those array indices, or else make sure allifconditions first convert the string to number with the unary plus, like you have done in some assignments. This could well be a duplicate of issue with comparing two numbers in javascriptgoalsF=+goalsF + +Results[i][1];and also how you need to compare the "results" values/what those values meanResultsarray via a html table that collects user input. Given this how do I store only number data types within the array?goalsF=+goalsF + +Results[i]counts all the goals for (or goals scored by) a player. So it takes the goals scored in the current result and adds them to the total. This is for goalsA (goals against). This is used to calculate goal difference and later sort the league table. And lastly the resaon I compare the results values is to see which player is the winner ie the player with the greater number of goals wins the match, the player with the least is the loser, and if they're equal it's a draw of course.