Skip to content

Instantly share code, notes, and snippets.

@thunder775
Last active October 7, 2020 08:38
Show Gist options
  • Save thunder775/cd5e94f463e97044638f159b8405f13b to your computer and use it in GitHub Desktop.
Save thunder775/cd5e94f463e97044638f159b8405f13b to your computer and use it in GitHub Desktop.
function tournamentScores(matchArray = ['', '']) {
let teamDataMap = new Map();
matchArray.forEach((matchString) => {
let [team1, team2] = matchString.split(' - ');
let [team1Name,team1Goals] = team1.split(' ');
let [team2Name,team2Goals] = team1.split(' ');
addToMap(teamDataMap,team1Name,Number(team1Goals),Number(team2Goals));
addToMap(teamDataMap,team2Name,Number(team2Goals),Number(team1Goals));
});
let result = [...teamDataMap.entries()].map(array => Array.from([array[0]]).concat(array[1]));
result.sort(([team1, pt1, gs1, gd1], [team2, pt2, gs2, gd2]) => {
if (pt1 === pt2) {
if (gs2 === gs1) {
return (gd1 - gd2);
}
return (gs1 - gs2);
}
return pt1-pt2;
});
return result;
}
function addToMap(teamDataMap = new Map(), team1Name = '', team1Goals = 1, team2Goals = 1) {
if (teamDataMap.get(team1Name) !== undefined) {
let teamData = teamDataMap.get(team1Name);
teamData[0] += team1Goals > team2Goals ? 3 : (team1Goals === team2Goals ? 1 : 0);
teamData[1] += team1Goals;
teamData[2] += team1Goals - team2Goals;
teamDataMap.set(team1Name, teamData)
} else {
teamDataMap.set(team1Name, [team1Goals > team2Goals ? 3 : (team1Goals === team2Goals ? 1 : 0), team1Goals, team1Goals-team2Goals])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment