satyacode
Background
Back to js-functions

02-cricket-stats.js

JavaScript
1/**
2 * 🏏 Cricket Player Stats Dashboard
3 *
4 * IPL ka stats dashboard bana raha hai tu! Har function ARROW FUNCTION
5 * hona chahiye (const fn = () => ...). Regular function declarations
6 * mat use karna — arrow functions ki practice karna hai!
7 *
8 * Functions (sab arrow functions honge):
9 *
10 *   1. calcStrikeRate(runs, balls)
11 *      - Strike rate = (runs / balls) * 100, rounded to 2 decimal places
12 *      - Agar balls <= 0 ya runs < 0, return 0
13 *
14 *   2. calcEconomy(runsConceded, overs)
15 *      - Economy = runsConceded / overs, rounded to 2 decimal places
16 *      - Agar overs <= 0 ya runsConceded < 0, return 0
17 *
18 *   3. calcBattingAvg(totalRuns, innings, notOuts = 0)
19 *      - Batting avg = totalRuns / (innings - notOuts), rounded to 2 decimal places
20 *      - Default notOuts = 0
21 *      - Agar innings - notOuts <= 0, return 0
22 *
23 *   4. isAllRounder(battingAvg, economy)
24 *      - Return true agar battingAvg > 30 AND economy < 8
25 *
26 *   5. getPlayerCard(player)
27 *      - player object: { name, runs, balls, totalRuns, innings, notOuts, runsConceded, overs }
28 *      - Return: { name, strikeRate, economy, battingAvg, isAllRounder }
29 *      - Use the above functions internally
30 *      - Agar player null/undefined hai ya name missing, return null
31 *
32 * Hint: Use const fn = (params) => expression or const fn = (params) => { ... }
33 *
34 * @example
35 *   calcStrikeRate(45, 30)  // => 150
36 *   calcEconomy(24, 4)      // => 6
37 *   getPlayerCard({ name: "Jadeja", runs: 35, balls: 20, totalRuns: 2000, innings: 80, notOuts: 10, runsConceded: 1500, overs: 200 })
38 *   // => { name: "Jadeja", strikeRate: 175, economy: 7.5, battingAvg: 28.57, isAllRounder: false }
39 */
40export const calcStrikeRate = (runs, balls) => {
41  let strikeRate = (runs/balls)*100
42  if(strikeRate % 1 !== 0){
43    strikeRate = Math.round(strikeRate * 100)/100
44  }
45  return balls <= 0 || runs < 0 ? 0 : strikeRate
46};
47
48export const calcEconomy = (runsConceded, overs) => {
49  if(overs <= 0 || runsConceded < 0){
50    return 0
51  } 
52  let economy = runsConceded / overs;
53  return Math.round(economy * 100) / 100; 
54}
55
56
57export const calcBattingAvg = (totalRuns, innings, notOuts = 0) => {
58  if( innings - notOuts <= 0){
59    return 0
60  }
61  
62  let battingAvg = totalRuns/(innings - notOuts)
63  if(battingAvg % 1 !== 0){
64    battingAvg = Math.round(battingAvg*100)/100
65  }
66  return battingAvg
67};
68
69export const isAllRounder = (battingAvg, economy) => {
70  return battingAvg > 30 && economy < 8
71};
72
73export const getPlayerCard = (player) => {
74
75  if(player == null|| player == undefined){
76    return null
77  }
78
79  if(!player.name){
80    return null
81  }
82  const { name, runs, balls, totalRuns, innings, notOuts, runsConceded, overs } = player
83  const strikeRate = calcStrikeRate(runs, balls)
84  const economy = calcEconomy(runsConceded,overs);
85  const battingAvg = calcBattingAvg(totalRuns, innings, notOuts)
86  const AllRounder = isAllRounder(battingAvg, economy)
87
88  return { name, strikeRate, economy, battingAvg, isAllRounder:AllRounder }
89};
90