satyacode
Background
JavaScript
1/**
2 * 🛺 Auto Rickshaw Fare Calculator - Number & Math
3 *
4 * Bhaiyya ji ka auto rickshaw hai. Meter se fare calculate hota hai.
5 * Different math operations chahiye — round karna, min/max nikalna,
6 * strings se numbers parse karna. Tu Bhaiyya ji ka meter software bana!
7 *
8 * Methods to explore: parseFloat(), parseInt(), .toFixed(),
9 *   Math.ceil(), Math.max(), Math.min(), Math.abs()
10 *
11 * Functions:
12 *
13 *   1. parseFare(fareString)
14 *      - Customer bolte hain "152.50" as string — parseFloat() se number banao
15 *      - Agar result NaN hai ya fareString string nahi hai, return -1
16 *      - Example: parseFare("152.50") => 152.5
17 *      - Example: parseFare("abc") => -1
18 *
19 *   2. roundFare(amount, decimalPlaces)
20 *      - .toFixed(decimalPlaces) se fare round karo
21 *      - Result STRING return hota hai (toFixed returns string)
22 *      - Agar amount number nahi hai ya decimalPlaces non-negative integer nahi hai, return ""
23 *      - Example: roundFare(152.567, 2) => "152.57"
24 *      - Example: roundFare(152.567, 0) => "153"
25 *
26 *   3. calculateSurge(baseFare, surgeMultiplier)
27 *      - baseFare * surgeMultiplier karo
28 *      - Math.ceil() se always round UP (auto wale ko paisa milna chahiye!)
29 *      - Agar baseFare ya surgeMultiplier positive number nahi hai, return 0
30 *      - Example: calculateSurge(100, 1.5) => 150
31 *      - Example: calculateSurge(73, 1.8) => 132 (Math.ceil(131.4))
32 *
33 *   4. findCheapestAndCostliest(...fares)
34 *      - Rest parameter (...) se variable number of fares le
35 *      - Math.min() aur Math.max() se cheapest aur costliest dhundho
36 *      - Non-number values filter out karo
37 *      - Agar koi valid number nahi mila, return null
38 *      - Return: { cheapest, costliest }
39 *      - Example: findCheapestAndCostliest(150, 80, 200) => { cheapest: 80, costliest: 200 }
40 *
41 *   5. getDistanceDifference(from, to)
42 *      - parseInt() se string km markers ko numbers mein convert karo
43 *      - Math.abs() se absolute difference nikalo (direction matter nahi karta)
44 *      - Agar parse ke baad koi NaN hai, return -1
45 *      - Example: getDistanceDifference(5, 12) => 7
46 *      - Example: getDistanceDifference("15", "8") => 7
47 *
48 * @example
49 *   parseFare("152.50")                    // => 152.5
50 *   roundFare(152.567, 2)                  // => "152.57"
51 *   findCheapestAndCostliest(150, 80, 200) // => { cheapest: 80, costliest: 200 }
52 */
53export function parseFare(fareString) {
54  if (typeof fareString !== "string") {
55    return -1;
56  }
57
58  const result = parseFloat(fareString);
59
60  if (Number.isNaN(result)) {
61    return -1;
62  }
63
64  return result;
65}
66
67export function roundFare(amount, decimalPlaces) {
68  if (
69    typeof amount !== "number" ||
70    !Number.isInteger(decimalPlaces) ||
71    decimalPlaces < 0
72  ) {
73    return "";
74  }
75
76  return amount.toFixed(decimalPlaces);
77}
78
79
80export function calculateSurge(baseFare, surgeMultiplier) {
81  if(baseFare < 0 || surgeMultiplier < 0|| typeof baseFare !== "number" || typeof surgeMultiplier !== "number"){
82    return 0
83  }
84  return Math.ceil(baseFare*surgeMultiplier)
85}
86
87export function findCheapestAndCostliest(...fares) {
88
89const filteredFair = fares.filter(f => typeof f == "number" && !Number.isNaN(f))
90
91    if (filteredFair.length === 0) {
92    return null;
93  }
94
95  let cheapest = Math.min(...filteredFair);
96  let costliest = Math.max(...filteredFair);
97
98  return {cheapest, costliest}
99
100}
101
102export function getDistanceDifference(from, to) {
103   let fromINT = parseInt(from)
104   let toINT = parseInt(to)
105
106   let value = Math.abs(fromINT - toINT)
107
108   if(Number.isNaN(value)){
109    return -1
110   }
111
112   return value
113
114}
115