satyacode
Background
Back to js-functions

04-bollywood-director.js

JavaScript
1/**
2 * 🎬 Bollywood Scene Director - Factory Functions
3 *
4 * Bollywood ka script generator bana! Factory functions use karo — matlab
5 * aise functions jo DOOSRE functions return karte hain. Pehle configuration
6 * do, phir ek specialized function milega jo kaam karega.
7 *
8 * Functions:
9 *
10 *   1. createDialogueWriter(genre)
11 *      - Factory: returns a function (hero, villain) => string
12 *      - Genres and their dialogue templates:
13 *        "action"  => `${hero} says: 'Tujhe toh main dekh lunga, ${villain}!'`
14 *        "romance" => `${hero} whispers: '${villain}, tum mere liye sab kuch ho'`
15 *        "comedy"  => `${hero} laughs: '${villain} bhai, kya kar rahe ho yaar!'`
16 *        "drama"   => `${hero} cries: '${villain}, tune mera sab kuch cheen liya!'`
17 *      - Unknown genre => return null (not a function, just null)
18 *      - Returned function: if hero or villain empty/missing, return "..."
19 *
20 *   2. createTicketPricer(basePrice)
21 *      - Factory: returns a function (seatType, isWeekend = false) => price
22 *      - Seat multipliers: silver=1, gold=1.5, platinum=2
23 *      - Agar isWeekend, multiply final price by 1.3 (30% extra)
24 *      - Round to nearest integer
25 *      - Unknown seatType in returned fn => return null
26 *      - Agar basePrice not positive number => return null (not a function)
27 *
28 *   3. createRatingCalculator(weights)
29 *      - Factory: returns a function (scores) => weighted average
30 *      - weights: { story: 0.3, acting: 0.3, direction: 0.2, music: 0.2 }
31 *      - scores: { story: 8, acting: 9, direction: 7, music: 8 }
32 *      - Weighted avg = sum of (score * weight) for matching keys
33 *      - Round to 1 decimal place
34 *      - Agar weights not an object => return null
35 *
36 * Hint: A factory function RETURNS another function. The returned function
37 *   "remembers" the parameters of the outer function (this is a closure!).
38 *
39 * @example
40 *   const actionWriter = createDialogueWriter("action");
41 *   actionWriter("Shah Rukh", "Raees")
42 *   // => "Shah Rukh says: 'Tujhe toh main dekh lunga, Raees!'"
43 *
44 *   const pricer = createTicketPricer(200);
45 *   pricer("gold", true)  // => 200 * 1.5 * 1.3 = 390
46 */
47export function createDialogueWriter(genre) {
48  const templates = {
49    action: (hero, villain) =>
50      `${hero} says: 'Tujhe toh main dekh lunga, ${villain}!'`,
51    romance: (hero, villain) =>
52      `${hero} whispers: '${villain}, tum mere liye sab kuch ho'`,
53    comedy: (hero, villain) =>
54      `${hero} laughs: '${villain} bhai, kya kar rahe ho yaar!'`,
55    drama: (hero, villain) =>
56      `${hero} cries: '${villain}, tune mera sab kuch cheen liya!'`,
57  }
58
59  if (!templates[genre]) {
60    return null
61  }
62
63  return function (hero, villain) {
64    if (!hero || !villain) {
65      return "..."
66    }
67
68    return templates[genre](hero, villain)
69  }
70}
71
72export function createTicketPricer(basePrice) {
73
74  if( typeof basePrice !== 'number' || basePrice <= 0){
75    return null
76  }
77
78  const seatMultiplier = {
79    silver: 1,
80    gold: 1.5,
81    platinum: 2
82  }
83  
84  return function(seatType, isWeekend = false){
85    if(!seatMultiplier[seatType]){
86      return null
87    }
88
89    let price = basePrice*seatMultiplier[seatType]
90
91    if(isWeekend){
92      price *= 1.3
93    }
94
95    return Math.round(price)
96  }
97}
98
99export function createRatingCalculator(weights) {
100  if(typeof weights !== "object" || !weights){
101    return null
102  }
103
104  return function(scores){
105    let sum = 0;
106
107   for(let key in weights){
108    sum += weights[key] * scores[key]
109   }
110
111   return sum
112  }
113}
114