Definitely some things missing and room for improvement but here we go. JavaScript.
const calcPoints = Symbol('calcPoints'); class LeagueTable { constructor() { this.matches = []; this.teamMapping = null; } [calcPoints](a, b) { return a > b ? 3 : a === b ? 1 : 0; } push(matchString) { let rawScore = matchString.match(/\d+\s*\-\s*\d+/g)[0]; let teamGoals = rawScore.split('-').map(x => parseInt(x.trim())); let names = matchString.split(rawScore).map(x => x.trim()); if (!this.teamMapping) { this.teamMapping = names.map((x, i) => ({ key: i, value: x })); } let teamOne = { teamId: this.teamMapping.find(x => x.value === names[0]).key, goals: teamGoals[0], gd: teamGoals[0] - teamGoals[1], points: this[calcPoints](teamGoals[0], teamGoals[1]) }; let teamTwo = { teamId: this.teamMapping.find(x => x.value === names[1]).key, goals: teamGoals[1], gd: teamGoals[1] - teamGoals[0], points: this[calcPoints](teamGoals[1], teamGoals[0]) }; this.matches.push(teamOne); this.matches.push(teamTwo); } getPoints(teamName) { let teamId = this.teamMapping.find(x => x.value === teamName).key; return this.matches .filter(x => x.teamId === teamId) .reduce((acc, v) => acc + v.points, 0); } getGoalsFor(teamName) { let teamId = this.teamMapping.find(x => x.value === teamName).key; return this.matches .filter(x => x.teamId === teamId) .reduce((acc, v) => acc + v.goals, 0); } getGoalsAgainst(teamName) { let teamId = this.teamMapping.find(x => x.value !== teamName).key; return this.matches .filter(x => x.teamId === teamId) .reduce((acc, v) => acc + v.goals, 0); } getGoalDifference(teamName) { let teamId = this.teamMapping.find(x => x.value !== teamName).key; return this.matches .filter(x => x.teamId === teamId) .reduce((acc, v) => acc + -v.gd, 0); } getWins(teamName) { let teamId = this.teamMapping.find(x => x.value === teamName).key; return this.matches .filter(x => x.teamId === teamId) .reduce((acc, v) => (v.points > 1 ? acc + 1 : acc), 0); } getDraws(teamName) { let teamId = this.teamMapping.find(x => x.value === teamName).key; return this.matches .filter(x => x.teamId === teamId) .reduce((acc, v) => (v.points === 1 ? acc + 1 : acc), 0); } getLosses(teamName) { let teamId = this.teamMapping.find(x => x.value === teamName).key; return this.matches .filter(x => x.teamId === teamId) .reduce((acc, v) => (v.points === 0 ? acc + 1 : acc), 0); } } let lt = new LeagueTable(); lt.push('Man Utd 3 - 0 Liverpool'); console.log(`Goals : ${lt.getGoalsFor('Man Utd')}`); console.log(`Points : ${lt.getPoints('Man Utd')}`); console.log(`Points : ${lt.getPoints('Liverpool')}`); console.log(`Goal Difference : ${lt.getGoalDifference('Liverpool')}`); lt.push('Liverpool 1 - 1 Man Utd'); console.log(`Goals : ${lt.getGoalsFor('Man Utd')}`); console.log(`Points : ${lt.getPoints('Man Utd')}`); console.log(`Points : ${lt.getPoints('Liverpool')}`); console.log(`Goals Against : ${lt.getGoalsAgainst('Man Utd')}`);
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Definitely some things missing and room for improvement but here we go. JavaScript.