satyacode
Background
Back to js-loops

01-chai-tapri.js

JavaScript
1/**
2 * ☕ Raju ki Chai Tapri - Revenue Calculator
3 *
4 * Raju bhai apni chai tapri chalate hain station ke bahar. Subah se shaam tak
5 * customers aate hain aur Raju bhai sabko chai pilate hain. Ab Raju bhai ko
6 * apna daily revenue calculate karna hai.
7 *
8 * Raju bhai ka special rule:
9 *   - Har 3rd customer ko milti hai "Adrak Chai" (special ginger tea) = Rs 15
10 *   - Baaki sab ko milti hai "Cutting Chai" = Rs 10
11 *   - Customer 1 = cutting, Customer 2 = cutting, Customer 3 = adrak,
12 *     Customer 4 = cutting, Customer 5 = cutting, Customer 6 = adrak ... and so on
13 *
14 * Validation:
15 *   - Agar customers ek positive integer nahi hai (e.g., negative, zero, decimal,
16 *     string, etc.), toh return karo: { totalChai: 0, totalRevenue: 0 }
17 *
18 * @param {number} customers - Kitne customers aaye aaj
19 * @returns {{ totalChai: number, totalRevenue: number }} Total chai served and revenue earned
20 *
21 * @example
22 *   chaiTapriRevenue(6)
23 *   // => { totalChai: 6, totalRevenue: 70 }
24 *   // 4 cutting (4*10=40) + 2 adrak (2*15=30) = 70
25 *
26 *   chaiTapriRevenue(0)
27 *   // => { totalChai: 0, totalRevenue: 0 }
28 */
29export function chaiTapriRevenue(customers) {
30  if(customers < 1 || typeof customers !== "number" || !Number.isInteger(customers)){
31    return {totalChai:0, totalRevenue: 0}
32  }
33  let totalRevenue = 0;
34  for(let i = 1; i<= customers; i++){
35    if(i % 3 === 0){
36      totalRevenue += 15
37    }else{
38      totalRevenue += 10
39    }
40  }
41
42
43  return {totalChai:customers, totalRevenue}
44}
45