Finished
77 * @param {string} input
88 * @returns {string}
99 */
10export const tournamentTally = () => {
11 throw new Error('Remove this line and implement the function');
10export const tournamentTally = (input) => {
11 // Edge Case: Empty input
12 if (!input || input.trim() === '') {
13 return 'Team | MP | W | D | L | P';
14 }
15
16 const teams = new Map();
17
18 // Helper function to update team stats
19 const updateTeam = (teamName, played, won, drawn, lost, points) => {
20 if (!teams.has(teamName)) {
21 teams.set(teamName, { name: teamName, mp: 0, w: 0, d: 0, l: 0, p: 0 });
22 }
23 const team = teams.get(teamName);
24 team.mp += played;
25 team.w += won;
26 team.d += drawn;
27 team.l += lost;
28 team.p += points;
29 };
30
31 const lines = input.split('\n');
32
33 // Edge Case: Input with only whitespace or empty lines
34 if (lines.every(line => line.trim() === '')) {
35 return 'Team | MP | W | D | L | P';
36 }
37
38 for (const line of lines) {
39 // Edge Case: Skip empty lines
40 if (line.trim() === '') {
41 continue;
42 }
43
44 const parts = line.split(';');
45
46 // Edge Case: Invalid match format (not exactly 3 parts)
47 if (parts.length !== 3) {
48 continue;
49 }
50
51 const [team1, team2, result] = parts;
52
53 // Edge Case: Invalid match outcome
54 if (!['win', 'loss', 'draw'].includes(result)) {
55 continue;
56 }
57
58 switch (result) {
59 case 'win':
60 updateTeam(team1, 1, 1, 0, 0, 3);
61 updateTeam(team2, 1, 0, 0, 1, 0);
62 break;
63 case 'loss':
64 updateTeam(team1, 1, 0, 0, 1, 0);
65 updateTeam(team2, 1, 1, 0, 0, 3);
66 break;
67 case 'draw':
68 updateTeam(team1, 1, 0, 1, 0, 1);
69 updateTeam(team2, 1, 0, 1, 0, 1);
70 break;
71 }
72 }
73
74 // Edge Case: No valid matches found
75 if (teams.size === 0) {
76 return 'Team | MP | W | D | L | P';
77 }
78
79 // Convert map to array and sort
80 const sortedTeams = Array.from(teams.values()).sort((a, b) => {
81 // Sort by points descending
82 if (b.p !== a.p) {
83 return b.p - a.p;
84 }
85 // If points are equal, sort alphabetically
86 return a.name.localeCompare(b.name);
87 });
88
89 // Format the output
90 let output = 'Team | MP | W | D | L | P';
91
92 for (const team of sortedTeams) {
93 output += `\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)}`;
94 }
95
96 return output;
1297};
98
99// Handled Edge Cases: Empty input, input with only whitespace, invalid match format, invalid match outcome, no valid matches found
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.