0

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:

Si rightly gets the win

For 10-2 (or 10-3) the result gets reversed and the player with the lower score is deemed the winner!?

Ste wrongly gets the victory!

It's as if the comparison is only working on the first digit of the scoreline. How do I fix this?

gdh48k
  • 29
  • 6
  • I don't see in your code where you assign values to `Results[i][1]` and `Results[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 all `if` conditions 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 javascript](https://stackoverflow.com/questions/9094299/issue-with-comparing-two-numbers-in-javascript) – trincot Aug 27 '19 at 20:04
  • Hi there, could you please elaborate what you are trying to do here: `goalsF=+goalsF + +Results[i][1];` and also how you need to compare the "results" values/what those values mean – Marc Sloth Eastman Aug 27 '19 at 20:06
  • Thanks @trincot. Values are assigned to the `Results` array via a html table that collects user input. Given this how do I store only number data types within the array? – gdh48k Aug 27 '19 at 22:33
  • Thank you @Marc Sloth Eastman. `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. – gdh48k Aug 27 '19 at 22:38
  • @gdh48k I'm sorry, I don't see the number for the goals. And I don't understand why you are doing so much string to number conversion. Can you explain the results array and what all the elements in the sub arrays stand for? – Marc Sloth Eastman Aug 28 '19 at 14:11

0 Answers0