Hey there hope all is well,
Just wrapping up a small coding assignment where i'd like to avoid repeating myself in the implementation
let matchScores = [
{
'0': { 'Team A': 3, 'Team B': 3 },
'1': { 'Team D': 1, 'Team F': 0 },
'2': { 'Team E': 2, 'Team C': 0 }
},
{
'3': { 'Team E': 1, 'Team F': 2 },
'4': { 'Team B': 0, 'Team D': 0 },
'5': { 'Team C': 4, 'Team A': 2 }
}
]
This is the input to the function which is array of objects that have a "match" results between two teams. Each element in the array is a different "day" of games that need to be appended to a total point score. The goal is to create a function that for each "day ill iterate through the array and sum up the points awarded for the winners of the matches.
If a team wins they are awarded 3 points, if there is a tie both teams are only awarded one point.
Expected Output
Match 1
Team A, 3 pts
Team B, 3 pts
Team C, 1 pt
Match 2
Team A, 4 pts
Team B, 3 pts
Team C, 3 pts
function calculateResults(matchDays) {
let matchScore = {};
matchDays.forEach((day, i) => {
for (const [key, value] of Object.entries(day)) {
let match = Object.entries(value);
let team1 = match[0];
let team2 = match[1];
// team 1 win
if (team1[1] > team2[1]) {
// if team's score exists
if (matchScore[team1[0]]) {
// add value
matchScore[team1[0]] = 3;
} else {
// create value
matchScore[team1[0]] = 3;
}
}
// team 2 win
if (team1[1] < team2[1]) {
// if team's score exists
if (matchScore[team2[0]]) {
matchScore[team2[0]] = 3;
} else {
matchScore[team2[0]] = 3;
}
}
// tie
if (team1[1] === team2[1]) {
if (matchScore[team1[0]]) {
matchScore[team1[0]] = 1;
} else {
matchScore[team1[0]] = 1;
}
if (matchScore[team2[0]]) {
matchScore[team2[0]] = 1;
} else {
matchScore[team2[0]] = 1;
}
}
}
});
}
Here's my current implementation. Is there any way I can avoid repeating the object key value checks if it exists? Im currently trying to iterate through and store the value in matchScore
CodePudding user response:
You can use the new(ish) nullish coalescing operator to convert undefined
to 0
, then just always do the addition:
// team 1 win
if (team1[1] > team2[1]) {
matchScore[team1[0]] = matchScore[team1[0]] ?? 0 3; // ***
}
// team 2 win
if (team1[1] < team2[1]) {
matchScore[team2[0]] = matchScore[team2[0]] ?? 0 3; // ***
}
// ...and so on...
??
evaluates its left-hand operand and, if that value is not undefined
, takes that value as its result; if the left-hand value is undefined
, ??
evaluates its right-hand operand and takes that value as its result.
If you need to target an environment that doesn't have ??
(yet), in this particular case you can use ||
(logical OR) instead. ||
does the same thing ??
does, but the check isn't just for undefined
, but for any falsy value. In this particular case, that's fine.
You probably want else if
and else
in there as well:
if (team1[1] > team2[1]) {
// team 1 win
matchScore[team1[0]] = matchScore[team1[0]] ?? 0 3;
} else if (team1[1] < team2[1]) {
// team 2 win
matchScore[team2[0]] = matchScore[team2[0]] ?? 0 3;
} else {
// tie
matchScore[team1[0]] = matchScore[team1[0]] ?? 0 1;
matchScore[team2[0]] = matchScore[team2[0]] ?? 0 1;
}