satyacode
Background
Back to js-functions

03-tiffin-service.js

JavaScript
1/**
2 * 🍱 Mumbai Tiffin Service - Plan Builder
3 *
4 * Mumbai ki famous tiffin delivery service hai. Customer ka plan banana hai
5 * using destructuring parameters aur rest/spread operators.
6 *
7 * Functions:
8 *
9 *   1. createTiffinPlan({ name, mealType = "veg", days = 30 })
10 *      - Destructured parameter with defaults!
11 *      - Meal prices per day: veg=80, nonveg=120, jain=90
12 *      - Agar mealType unknown hai, return null
13 *      - Agar name missing/empty, return null
14 *      - Return: { name, mealType, days, dailyRate, totalCost }
15 *
16 *   2. combinePlans(...plans)
17 *      - Rest parameter! Takes any number of plan objects
18 *      - Each plan: { name, mealType, days, dailyRate, totalCost }
19 *      - Return: { totalCustomers, totalRevenue, mealBreakdown }
20 *      - mealBreakdown: { veg: count, nonveg: count, ... }
21 *      - Agar koi plans nahi diye, return null
22 *
23 *   3. applyAddons(plan, ...addons)
24 *      - plan: { name, mealType, days, dailyRate, totalCost }
25 *      - Each addon: { name: "raita", price: 15 }
26 *      - Add each addon price to dailyRate
27 *      - Recalculate totalCost = new dailyRate * days
28 *      - Return NEW plan object (don't modify original)
29 *      - addonNames: array of addon names added
30 *      - Agar plan null hai, return null
31 *
32 * Hint: Use { destructuring } in params, ...rest for variable args,
33 *   spread operator for creating new objects
34 *
35 * @example
36 *   createTiffinPlan({ name: "Rahul" })
37 *   // => { name: "Rahul", mealType: "veg", days: 30, dailyRate: 80, totalCost: 2400 }
38 *
39 *   combinePlans(plan1, plan2, plan3)
40 *   // => { totalCustomers: 3, totalRevenue: 7200, mealBreakdown: { veg: 2, nonveg: 1 } }
41 */
42export function createTiffinPlan({ name, mealType = "veg", days = 30 } = {}) {
43    if(!name){
44      return null
45    }
46
47    const prices = {
48      veg: 80,
49      nonveg: 120,
50      jain: 90
51    }
52
53    if(!prices[mealType]){
54      return null
55    }
56
57    const dailyRate = prices[mealType]
58    const totalCost = dailyRate * days
59
60    return { name, mealType, days, dailyRate, totalCost }
61}
62
63export function combinePlans(...plans) {
64  if(plans.length === 0){
65    return null;
66  }
67  
68  const totalCustomers = plans.length;
69  const totalRevenue = plans.reduce((acc, curr) => (acc + curr.totalCost), 0);
70  const mealBreakdown = plans.reduce((acc, curr) => {
71    if(acc[curr.mealType]){
72      acc[curr.mealType] += 1;  
73    } else {
74      acc[curr.mealType] = 1;   
75    }
76    return acc;
77  }, {});
78
79  return { totalCustomers, totalRevenue, mealBreakdown };
80}
81
82export function applyAddons(plan, ...addons) {
83
84  if(!plan){
85    return null;
86  }
87
88  let newDailyRate = plan.dailyRate;
89  for(let addon of addons){
90    newDailyRate += addon.price;
91  }
92
93  const newTotalCost = newDailyRate * plan.days;
94
95  const addonNames = addons.map(addon => addon.name);
96
97  return {
98    ...plan,             
99    dailyRate: newDailyRate,   
100    totalCost: newTotalCost,   
101    addonNames: addonNames   
102  };
103}
104