satyacode
Background
Back to js-functions

01-dosa-counter.js

JavaScript
1/**
2 * 🍳 Dosa Counter - Order Calculator
3 *
4 * Raju ka South Indian dosa counter hai Bangalore mein. Customer aata hai,
5 * dosa ka type bolta hai, kitne chahiye bolta hai, aur spicy chahiye ya nahi.
6 * Tujhe order calculate karke bill banana hai.
7 *
8 * Rules:
9 *   - Dosa prices: plain=40, masala=60, onion=50, butter=70, paper=90, cheese=80
10 *   - quantity ka default value 1 hai (agar nahi diya toh 1 maano)
11 *   - isSpicy ka default value false hai
12 *   - Agar isSpicy true hai, toh har dosa pe Rs 10 extra lagao
13 *   - pricePerDosa = base price + (10 if spicy)
14 *   - total = pricePerDosa * quantity
15 *   - Return: { type, quantity, pricePerDosa, total }
16 *   - Hint: Use default parameters, object return
17 *
18 * Validation:
19 *   - Agar type string nahi hai ya unknown type hai, return null
20 *   - Agar quantity positive number nahi hai (<=0 ya NaN), return null
21 *
22 * @param {string} type - Dosa type
23 * @param {number} [quantity=1] - Number of dosas
24 * @param {boolean} [isSpicy=false] - Add spicy for Rs 10 extra
25 * @returns {{ type: string, quantity: number, pricePerDosa: number, total: number } | null}
26 *
27 * @example
28 *   calculateDosaOrder("masala", 2, true)
29 *   // => { type: "masala", quantity: 2, pricePerDosa: 70, total: 140 }
30 *
31 *   calculateDosaOrder("plain")
32 *   // => { type: "plain", quantity: 1, pricePerDosa: 40, total: 40 }
33 */
34export function calculateDosaOrder(type, quantity = 1, isSpicy = false) {
35  if(typeof type !== "string" || quantity <= 0 || typeof quantity !== "number"){
36    return null
37  }
38  let pricePerDosa = 0;
39
40  switch(type){
41    case "plain":
42      pricePerDosa = 40;
43      break;
44    case "masala":
45      pricePerDosa = 60;
46      break;
47    case "onion":
48      pricePerDosa = 50;
49      break;
50    case "butter":
51      pricePerDosa = 70;
52      break;
53    case "paper":
54      pricePerDosa = 90;
55      break;
56    case "cheese":
57      pricePerDosa = 80;
58      break;
59    default:
60      return null  
61  }
62
63  if(isSpicy){
64    pricePerDosa += 10
65  }
66   
67  const total = pricePerDosa * quantity
68
69  
70  return  { type, quantity, pricePerDosa, total }
71}
72