Pokreniseweb
Pokreniseweb

Reputation: 165

How to determine which team won and add to it 3 point if it won, 0 if lose, and 1 if draw

I have array of game results, I need to determine based on score how many points to add to teams. In this example France get 3 points, Croatia 0 and England and Spain both 1 point. How this can be implemented, can anyone help?

const matches = [
    {
        homeTeam: 'France',
        awayTeam: 'Croatia',
        score: '2:1',
        date: '18.01.2019'
    },
      {
        homeTeam: 'England',
        awayTeam: 'Spain',
        score: '1:1',
        date: '18.01.2019'
    }
];

function getRankings(games) {
    // ...implementation
} 

const footbalRankings = getRankings(matches);
console.log(footbalRankings);

The output should be as this:

[
    { team: 'France', points:  3 }
    { team: 'England', points: 1 }
    { team: 'Spain', points: 1 }
    { team: 'Croatia', points: 0 }
]

Upvotes: 2

Views: 214

Answers (2)

Nikhil
Nikhil

Reputation: 6641

Create a function calculatePoints() which takes match score and calculates points for both teams.

Use Map to keep track of total points for each team. Map also preserves insertion order of the keys.

Then convert the results from Map to desired array format and sort by points.

const matches = [{ homeTeam: 'France', awayTeam: 'Croatia', score: '2:1', date: '18.01.2019' }, { homeTeam: 'England', awayTeam: 'Spain', score: '1:1', date: '18.01.2019' } ];

function getRankings(matches) {
  let rankings = new Map();
  let result = [];

  matches.forEach(match => {
    let totalA = rankings.get(match.homeTeam) || 0;
    let totalB = rankings.get(match.awayTeam) || 0;

    let points = calculatePoints(match.score);

    rankings.set(match.homeTeam, totalA + points.homeTeam);
    rankings.set(match.awayTeam, totalB + points.awayTeam);
  });

  // Convert result to array format.
  for (let [key, value] of rankings.entries()) {
    result.push({ "team": key, "points": value });
  }
  
  // Sort results by points.
  return result.sort((a, b) => b.points - a.points);
}

// Takes match score and calculates points for both teams.
function calculatePoints(matchScore) {
  let [teamA, teamB] = matchScore.split(":");
  let pointsA = 0, pointsB = 0;

  if (teamA > teamB) {
    pointsA = 3;
  } else if (teamA < teamB) {
    pointsB = 3;
  } else {
    pointsA = 1;
    pointsB = 1;
  }

  return { "homeTeam": pointsA, "awayTeam": pointsB };
}

const footballRankings = getRankings(matches);

console.log(footballRankings);

Upvotes: 1

Dillan Wilding
Dillan Wilding

Reputation: 1121

const matches = [
  {
    homeTeam: 'France',
    awayTeam: 'Croatia',
    score: '2:1',
    date: '18.01.2019'
  }, {
    homeTeam: 'England',
    awayTeam: 'Spain',
    score: '1:1',
    date: '18.01.2019'
  }
];

function getRankings(games) {
  var ranks = {};
  games.forEach(game => {
    var [homeScore, awayScore] = game.score.split(':');
    if (!ranks[game.homeTeam]) ranks[game.homeTeam] = { name: game.homeTeam, points: 0 };
    if (!ranks[game.awayTeam]) ranks[game.awayTeam] = { name: game.awayTeam, points: 0 };
    if (homeScore > awayScore) {
      ranks[game.homeTeam].points += 3;
    } else if (awayScore > homeScore) {
      ranks[game.awayTeam].points += 3;
    } else {
      ranks[game.homeTeam].points++;
      ranks[game.awayTeam].points++;
    }
  });
  return Object.values(ranks).sort((a, b) => a.points > b.points ? -1 : 1);
} 
        
var footbalRankings = getRankings(matches);
console.log(footbalRankings);

Upvotes: 3

Related Questions