Last active
June 1, 2020 06:50
-
-
Save blacksheep557/823265a5c81c2d4f75ffbe02b3f1a42b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function tournamentScores(matchArray = ['', '']) { | |
| let teamDataMap = new Map(); | |
| matchArray.forEach((matchString) => { | |
| let [team1, team2] = matchString.split(' - '); | |
| let [team1Name, team2Name] = [team1.split(' ')[0], team2.split(' ')[1]]; | |
| let [team1Goals, team2Goals] = [Number(team1.split(' ')[1]), Number(team2.split(' ')[0])]; | |
| addToMap(teamDataMap, team1Name, team1Goals, team2Goals); | |
| addToMap(teamDataMap, team2Name, team2Goals, team1Goals); | |
| }); | |
| return Array.from(teamDataMap.entries()).sort(([teamA, statsA], [teamB, statsB]) => { | |
| if (statsA.totalPoints !== statsB.totalPoints) { | |
| return statsB.totalPoints - statsA.totalPoints; | |
| } | |
| if (statsA.totalGoalsScored !== statsB.totalGoalsScored) { | |
| return statsB.totalGoalsScored - statsA.totalGoalsScored; | |
| } | |
| return statsB.diffGoalsScored - statsA.diffGoalsScored; | |
| }).map(([teamName, {totalPoints, totalGoalsScored, diffGoalsScored}]) => [teamName, totalPoints, totalGoalsScored, diffGoalsScored]); | |
| } | |
| function addToMap(teamDataMap = new Map(), team1Name = '', team1Goals = 1, team2Goals = 1) { | |
| let teamData = teamDataMap.get(team1Name); | |
| if (teamData === undefined) { | |
| teamData = { | |
| totalPoints: team1Goals > team2Goals ? 3 : (team1Goals === team2Goals ? 1 : 0), | |
| totalGoalsScored: team1Goals, | |
| diffGoalsScored: team1Goals - team2Goals | |
| }; | |
| teamDataMap.set(team1Name, teamData); | |
| } else { | |
| teamData.totalPoints += team1Goals > team2Goals ? 3 : (team1Goals === team2Goals ? 1 : 0); | |
| teamData.totalGoalsScored += team1Goals; | |
| teamData.diffGoalsScored += team1Goals - team2Goals; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment