satyacode
Background
Back to js-loops

07-ipl-points-table.js

JavaScript
1/**
2 * 🏆 IPL Season Points Table
3 *
4 * IPL ka season chal raha hai aur tujhe points table banana hai!
5 * Tujhe match results ka array milega, aur tujhe har team ke points
6 * calculate karke sorted table return karna hai.
7 *
8 * Match result types:
9 *   - "win": Winning team gets 2 points, losing team gets 0
10 *   - "tie": Both teams get 1 point each
11 *   - "no_result": Both teams get 1 point each (rain/bad light)
12 *
13 * Each match object: { team1: "CSK", team2: "MI", result: "win", winner: "CSK" }
14 *   - For "tie" and "no_result", the winner field is absent or ignored
15 *
16 * Rules (use for loop with object accumulator):
17 *   - Loop through matches array
18 *   - Build an object accumulator: { "CSK": { team, played, won, lost, tied, noResult, points }, ... }
19 *   - After processing all matches, convert to array and sort:
20 *     1. By points DESCENDING
21 *     2. If points are equal, by team name ASCENDING (alphabetical)
22 *
23 * Validation:
24 *   - Agar matches array nahi hai ya empty hai, return []
25 *
26 * @param {Array<{team1: string, team2: string, result: string, winner?: string}>} matches
27 * @returns {Array<{team: string, played: number, won: number, lost: number, tied: number, noResult: number, points: number}>}
28 *
29 * @example
30 *   iplPointsTable([
31 *     { team1: "CSK", team2: "MI", result: "win", winner: "CSK" },
32 *     { team1: "RCB", team2: "CSK", result: "tie" },
33 *   ])
34 *   // CSK: played=2, won=1, tied=1, points=3
35 *   // MI: played=1, won=0, lost=1, points=0
36 *   // RCB: played=1, tied=1, points=1
37 *   // Sorted: CSK(3), RCB(1), MI(0)
38 */
39export function iplPointsTable(matches) {
40  if (!Array.isArray(matches) || matches.length === 0) {
41    return []
42  }
43  
44  let teams = {}
45  
46  for (let i = 0; i < matches.length; i++) {
47    const match = matches[i];
48    const team1 = match.team1;
49    const team2 = match.team2;
50
51    if (!teams[team1]) {
52      teams[team1] = {
53        team: team1,
54        played: 0,
55        won: 0,
56        lost: 0,
57        tied: 0,
58        noResult: 0,
59        points: 0,
60      }
61    }
62
63    if (!teams[team2]) {
64      teams[team2] = {
65        team: team2,
66        played: 0,
67        won: 0,
68        lost: 0,
69        tied: 0,
70        noResult: 0,
71        points: 0,
72      }
73    }
74
75    if(match.result === "win"){
76      const winner = match.winner;
77      const loser = winner === team1 ? team2 : team1;
78      
79      teams[winner].played += 1;
80      teams[winner].won += 1;
81      teams[winner].points += 2;
82      
83      teams[loser].played += 1;
84      teams[loser].lost += 1;
85      
86    } else if(match.result === "tie"){
87      teams[team1].played += 1;
88      teams[team1].tied += 1;
89      teams[team1].points += 1;
90      
91      teams[team2].played += 1;
92      teams[team2].tied += 1;
93      teams[team2].points += 1;
94      
95    } else if(match.result === "no_result"){
96      teams[team1].played += 1;
97      teams[team1].noResult += 1;
98      teams[team1].points += 1;
99      
100      teams[team2].played += 1;
101      teams[team2].noResult += 1;
102      teams[team2].points += 1;
103    }
104  }
105
106  const tableArray = Object.values(teams);
107  
108  tableArray.sort((a, b) => {
109    if(b.points !== a.points){
110      return b.points - a.points;
111    }
112    return a.team.localeCompare(b.team);
113  });
114  
115  return tableArray;
116}
117