satyacode
Background
Back to js-loops

10-biryani-order.js

JavaScript
1/**
2 * 🍗 Paradise Biryani Batch System
3 *
4 * Paradise restaurant mein biryani orders aate hain. Kitchen ek batch mein
5 * sirf 5 plates bana sakti hai. Agar ek order mein zyada plates hain toh
6 * woh multiple batches mein split hota hai.
7 *
8 * Rules (use do...while loop):
9 *   - orders is an array of numbers (plates per order): [3, 7, 2, ...]
10 *   - Process orders one by one (for each order, use do...while for batching)
11 *   - Each batch can have MAXIMUM 5 plates
12 *   - If an order has more than 5, split into batches:
13 *     e.g., order of 12 = batch(5) + batch(5) + batch(2) = 3 batches
14 *   - Track: totalBatches, totalPlates, ordersProcessed
15 *   - Skip orders that are not positive integers (0, negative, decimal, non-number)
16 *
17 * Validation:
18 *   - Agar orders array nahi hai ya empty hai,
19 *     return: { totalBatches: 0, totalPlates: 0, ordersProcessed: 0 }
20 *
21 * @param {number[]} orders - Array of plate counts per order
22 * @returns {{ totalBatches: number, totalPlates: number, ordersProcessed: number }}
23 *
24 * @example
25 *   biryaniBatchProcessor([3, 7, 2])
26 *   // Order 3: 1 batch (3 plates)
27 *   // Order 7: 2 batches (5 + 2 plates)
28 *   // Order 2: 1 batch (2 plates)
29 *   // => { totalBatches: 4, totalPlates: 12, ordersProcessed: 3 }
30 *
31 *   biryaniBatchProcessor([5, 10])
32 *   // Order 5: 1 batch (5 plates)
33 *   // Order 10: 2 batches (5 + 5 plates)
34 *   // => { totalBatches: 3, totalPlates: 15, ordersProcessed: 2 }
35 */
36export function biryaniBatchProcessor(orders) {
37  if(!Array.isArray(orders) || orders.length === 0){
38    return { totalBatches: 0, totalPlates: 0, ordersProcessed: 0 }
39  }
40  
41  let totalBatches = 0;
42  let totalPlates = 0;
43  let ordersProcessed = 0;
44
45  for(let order of orders){
46    if(typeof order !== "number" || order <= 0 || !Number.isInteger(order)){
47      continue
48    }else{
49      let remaining  = order
50      do{
51        if(remaining > 5){
52          totalBatches += 1
53          remaining -= 5 
54          totalPlates += 5
55        }else{
56          totalBatches += 1
57          totalPlates += remaining
58          remaining = 0
59        }
60      }while(remaining  > 0)
61    }
62   ordersProcessed ++
63  }
64
65  return { totalBatches, totalPlates, ordersProcessed }
66}
67