satyacode
Background
JavaScript
1
2/**
3 * 🧾 GST Calculator - Tax Lagao Bhai!
4 *
5 * Bunty apni dukaan ke liye GST calculator bana raha hai. Customer ko bill
6 * dena hai jisme base price, GST amount, aur total clearly dikhna chahiye.
7 * GST rate category ke hisaab se change hota hai.
8 *
9 * GST Rates (by category string, case-insensitive):
10 *   - "essential"   => 0% GST  (dal, chawal, atta, etc.)
11 *   - "food"        => 5% GST  (packaged food, restaurant below Rs 7500)
12 *   - "standard"    => 12% GST (processed food, business class tickets)
13 *   - "electronics" => 18% GST (phones, laptops, etc.)
14 *   - "luxury"      => 28% GST (cars, aerated drinks, tobacco)
15 *   - Any other category => return null
16 *
17 * Rules:
18 *   - Calculate: gstAmount = amount * rate / 100
19 *   - Calculate: totalAmount = amount + gstAmount
20 *   - Round gstAmount aur totalAmount to 2 decimal places using
21 *     parseFloat(value.toFixed(2))
22 *   - Return object: { baseAmount, gstRate, gstAmount, totalAmount }
23 *   - category ko lowercase mein compare karo (case-insensitive)
24 *   - Hint: Use toFixed(), parseFloat(), Number.isFinite(), toLowerCase()
25 *
26 * Validation:
27 *   - Agar amount positive finite number nahi hai, return null
28 *   - Agar category string nahi hai, return null
29 *   - Agar category unknown hai, return null
30 *
31 * @param {number} amount - Base amount before tax
32 * @param {string} category - Product category
33 * @returns {{ baseAmount: number, gstRate: number, gstAmount: number, totalAmount: number } | null}
34 *
35 * @example
36 *   calculateGST(1000, "electronics")
37 *   // => { baseAmount: 1000, gstRate: 18, gstAmount: 180, totalAmount: 1180 }
38 *
39 *   calculateGST(500, "essential")
40 *   // => { baseAmount: 500, gstRate: 0, gstAmount: 0, totalAmount: 500 }
41 */
42export function calculateGST(amount, category) {
43
44  if(amount <= 0 || typeof category !== "string" || typeof amount === "string" || !Number.isFinite(amount) ){
45    return null
46  }
47
48  let gst = 0
49  category = category.toLowerCase()
50  switch(category){
51    case "essential":
52      gst = 0;
53      break;
54    case "food":
55      gst = 5;
56      break;
57    case "standard":
58      gst = 12;
59      break;
60    case "electronics":
61      gst = 18;
62      break;
63    case "luxury":
64      gst = 28;
65      break;
66    default:
67      return null        
68  }
69
70  let gstAmount = amount * gst/100;
71  gstAmount =   parseFloat(gstAmount.toFixed(2))
72  
73  let totalAmount = amount + parseFloat(gstAmount.toFixed(2))
74
75  let value = parseFloat(totalAmount.toFixed(2))
76
77  return {
78    baseAmount: amount,
79    gstRate: gst,
80    gstAmount,
81    totalAmount: value
82  }
83}
84