Finished
33// convenience to get you started writing code faster.
44//
55
6export const tournamentTally = () => {
7 throw new Error('Remove this line and implement the function');
6export const tournamentTally = (input) => {
7 // Edge Case: Empty input
8 if (!input || input.trim() === '') {
9 return 'Team | MP | W | D | L | P';
10 }
11
12 const teams = {};
13
14 const lines = input.split('\n');
15
16 // Edge Case: Malformed input lines
17 for (const line of lines) {
18 if (line.trim() === '') continue;
19
20 const parts = line.split(';');
21 if (parts.length !== 3) continue;
22
23 const [team1, team2, result] = parts;
24
25 // Edge Case: Invalid result value
26 if (!['win', 'loss', 'draw'].includes(result)) continue;
27
28 if (!teams[team1]) {
29 teams[team1] = { name: team1, mp: 0, w: 0, d: 0, l: 0, p: 0 };
30 }
31
32 if (!teams[team2]) {
33 teams[team2] = { name: team2, mp: 0, w: 0, d: 0, l: 0, p: 0 };
34 }
35
36 teams[team1].mp += 1;
37 teams[team2].mp += 1;
38
39 if (result === 'win') {
40 teams[team1].w += 1;
41 teams[team1].p += 3;
42 teams[team2].l += 1;
43 } else if (result === 'loss') {
44 teams[team1].l += 1;
45 teams[team2].w += 1;
46 teams[team2].p += 3;
47 } else if (result === 'draw') {
48 teams[team1].d += 1;
49 teams[team1].p += 1;
50 teams[team2].d += 1;
51 teams[team2].p += 1;
52 }
53 }
54
55 // Edge Case: No valid matches found
56 if (Object.keys(teams).length === 0) {
57 return 'Team | MP | W | D | L | P';
58 }
59
60 const teamArray = Object.values(teams);
61
62 // Sort by points (descending) and then by name (ascending)
63 teamArray.sort((a, b) => {
64 if (b.p !== a.p) {
65 return b.p - a.p;
66 }
67 return a.name.localeCompare(b.name);
68 });
69
70 let result = 'Team | MP | W | D | L | P';
71
72 for (const team of teamArray) {
73 result += `\n${team.name.padEnd(30)} | ${team.mp.toString().padStart(2)} | ${team.w.toString().padStart(2)} | ${team.d.toString().padStart(2)} | ${team.l.toString().padStart(2)} | ${team.p.toString().padStart(2)}`;
74 }
75
76 // Handled Edge Cases: Empty input, malformed input lines, invalid result value, no valid matches found
77 return result;
878};
Test NameStatus
just the header if no input
Pass
a win is three points, a loss is zero points
Pass
a win can also be expressed as a loss
Pass
a different team can win
Pass
a draw is one point each
Pass
there can be more than one match
Pass
there can be more than one winner
Pass
there can be more than two teams
Pass
typical input
Pass
incomplete competition (not all pairs have played)
Pass
ties broken alphabetically
Pass
ensure points sorted numerically
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.