satyacode
Background
JavaScript
1/**
2 * 🍽️ Thali Combo Platter - Mixed Methods Capstone
3 *
4 * Grand Indian Thali restaurant mein combo platter system banana hai.
5 * String, Number, Array, aur Object — sab methods mila ke ek complete
6 * thali banao. Yeh capstone challenge hai — sab kuch combine karo!
7 *
8 * Data format: thali = {
9 *   name: "Rajasthani Thali",
10 *   items: ["dal baati", "churma", "papad"],
11 *   price: 250,
12 *   isVeg: true
13 * }
14 *
15 * Functions:
16 *
17 *   1. createThaliDescription(thali)
18 *      - Template literal, .join(", "), .toUpperCase(), .toFixed(2) use karo
19 *      - Format: "{NAME} (Veg/Non-Veg) - Items: {items joined} - Rs.{price}"
20 *      - name ko UPPERCASE karo, price ko 2 decimal places tak
21 *      - isVeg true hai toh "Veg", false hai toh "Non-Veg"
22 *      - Agar thali object nahi hai ya required fields missing hain, return ""
23 *      - Required fields: name (string), items (array), price (number), isVeg (boolean)
24 *      - Example: createThaliDescription({name:"Rajasthani Thali", items:["dal","churma"], price:250, isVeg:true})
25 *                 => "RAJASTHANI THALI (Veg) - Items: dal, churma - Rs.250.00"
26 *
27 *   2. getThaliStats(thalis)
28 *      - Array of thali objects ka stats nikalo
29 *      - .filter() se veg/non-veg count
30 *      - .reduce() se average price
31 *      - Math.min/Math.max se cheapest/costliest
32 *      - .map() se saare names
33 *      - Return: { totalThalis, vegCount, nonVegCount, avgPrice (2 decimal string),
34 *                  cheapest (number), costliest (number), names (array) }
35 *      - Agar thalis array nahi hai ya empty hai, return null
36 *
37 *   3. searchThaliMenu(thalis, query)
38 *      - .filter() + .includes() se search karo (case-insensitive)
39 *      - Thali match karti hai agar name ya koi bhi item query include kare
40 *      - Agar thalis array nahi hai ya query string nahi hai, return []
41 *      - Example: searchThaliMenu(thalis, "dal") => thalis with "dal" in name or items
42 *
43 *   4. generateThaliReceipt(customerName, thalis)
44 *      - Template literals + .map() + .join("\n") + .reduce() se receipt banao
45 *      - Format:
46 *        "THALI RECEIPT\n---\nCustomer: {NAME}\n{line items}\n---\nTotal: Rs.{total}\nItems: {count}"
47 *      - Line item: "- {thali name} x Rs.{price}"
48 *      - customerName UPPERCASE mein
49 *      - Agar customerName string nahi hai ya thalis array nahi hai/empty hai, return ""
50 *
51 * @example
52 *   createThaliDescription({name:"Rajasthani Thali", items:["dal"], price:250, isVeg:true})
53 *   // => "RAJASTHANI THALI (Veg) - Items: dal - Rs.250.00"
54 */
55export function createThaliDescription(thali) {
56  if (typeof thali !== "object" || thali === null) {
57    return "";
58  }
59
60  const { name, items, price, isVeg } = thali;
61
62  if (
63    typeof name !== "string" ||
64    !Array.isArray(items) ||
65    typeof price !== "number" ||
66    typeof isVeg !== "boolean"
67  ) {
68    return "";
69  }
70
71  const formattedName = name.toUpperCase();
72  const formattedPrice = price.toFixed(2);
73  const dishType = isVeg ? "Veg" : "Non-Veg";
74  const itemList = items.join(", ");
75
76  return `${formattedName} (${dishType}) - Items: ${itemList} - Rs.${formattedPrice}`;
77}
78
79export function getThaliStats(thalis){
80if (!Array.isArray(thalis) || thalis.length === 0) {
81  return null;
82}
83
84  const totalThalis = thalis.length;
85  const vegCount = thalis.filter(item => (item.isVeg)).length;
86  const nonVegCount  = thalis.filter(item => (!item.isVeg)).length;
87  const sum = thalis.reduce((sum, t)=> sum + t.price, 0);
88  const avgPrice =(sum/totalThalis).toFixed(2);
89  const price = thalis.map(t=>t.price)
90  const cheapest = Math.min(...price);
91  const costliest = Math.max(...price);
92  const names = thalis.map(t=> t.name)
93   return {
94    totalThalis,
95    vegCount,
96    nonVegCount,
97    avgPrice,
98    cheapest,
99    costliest,
100    names,
101
102   }
103}
104
105export function searchThaliMenu(thalis, query) {
106  if (!Array.isArray(thalis) || typeof query !== "string") {
107    return [];
108  }
109
110  const search = query.toLowerCase();
111
112  return thalis.filter(thali => {
113    const nameMatch = thali.name
114      .toLowerCase()
115      .includes(search);
116
117    const itemMatch = thali.items.some(item =>
118      item.toLowerCase().includes(search)
119    );
120
121    return nameMatch || itemMatch;
122  });
123}
124
125
126export function generateThaliReceipt(customerName, thalis) {
127  if (
128    typeof customerName !== "string" ||
129    !Array.isArray(thalis) ||
130    thalis.length === 0
131  ) {
132    return "";
133  }
134
135  const customer = customerName.toUpperCase();
136
137  const lineItems = thalis
138    .map(t => `- ${t.name} x Rs.${t.price}`)
139    .join("\n");
140
141  const total = thalis.reduce((sum, t) => sum + t.price, 0);
142  const count = thalis.length;
143
144  return `THALI RECEIPT --- Customer: ${customer} ${lineItems} --- Total: Rs.${total} Items: ${count}`;
145}
146
147