Back to js-datatype-foundation
06-kiryana-store.js
JavaScript
1/**
2 * 🏪 Kiryana Store Bill - Array Transform
3 *
4 * Gupta ji ki kiryana (grocery) store hai. Monthly hisaab kitaab karna hai —
5 * items ka total nikalna, sorting karna, bill format karna.
6 * Array transform methods se Gupta ji ki dukaan digital banao!
7 *
8 * Data format: items = [
9 * { name: "Atta", price: 40, qty: 2 },
10 * { name: "Daal", price: 80, qty: 1 },
11 * ...
12 * ]
13 *
14 * Methods to explore: .map(), .filter(), .reduce(), .sort(), .join()
15 *
16 * Functions:
17 *
18 * 1. getItemNames(items)
19 * - .map() se sirf names nikalo
20 * - Agar items array nahi hai, return []
21 * - Example: getItemNames([{name:"Atta",price:40,qty:2}]) => ["Atta"]
22 *
23 * 2. getAffordableItems(items, maxPrice)
24 * - .filter() se items nikalo jinka price <= maxPrice
25 * - Agar items array nahi hai ya maxPrice number nahi hai, return []
26 * - Example: getAffordableItems([{name:"Atta",price:40},{name:"Ghee",price:500}], 100)
27 * => [{name:"Atta",price:40}]
28 *
29 * 3. calculateTotal(items)
30 * - .reduce() se (price * qty) ka sum nikalo
31 * - Agar items array nahi hai ya empty hai, return 0
32 * - Example: calculateTotal([{name:"Atta",price:40,qty:2},{name:"Daal",price:80,qty:1}])
33 * => 160
34 *
35 * 4. sortByPrice(items, ascending)
36 * - [...items].sort() se NEW sorted array return karo (original mat badlo!)
37 * - ascending = true => low to high, false => high to low
38 * - Agar items array nahi hai, return []
39 * - Example: sortByPrice([{name:"Ghee",price:500},{name:"Atta",price:40}], true)
40 * => [{name:"Atta",price:40},{name:"Ghee",price:500}]
41 *
42 * 5. formatBill(items)
43 * - .map() se har item ko "name x qty = Rs.total" format karo
44 * - Phir .join("\n") se multi-line bill banao
45 * - Agar items array nahi hai ya empty hai, return ""
46 * - Example: formatBill([{name:"Atta",price:40,qty:2}]) => "Atta x 2 = Rs.80"
47 *
48 * @example
49 * getItemNames([{name:"Atta",...}]) // => ["Atta"]
50 * calculateTotal([{price:40,qty:2},...]) // => 160
51 * formatBill([{name:"Atta",price:40,qty:2}]) // => "Atta x 2 = Rs.80"
52 */
53export function getItemNames(items) {
54 if(!Array.isArray(items)){
55 return []
56 }
57 return items.map(item => item.name)
58}
59
60export function getAffordableItems(items, maxPrice) {
61 if(!Array.isArray(items) || typeof maxPrice !== "number"){
62 return []
63 }
64 return items.filter(item => item.price <= maxPrice)
65}
66
67export function calculateTotal(items) {
68
69 if(!Array.isArray(items) || items.length == 0){
70 return 0
71 }
72
73 return items.reduce(
74 (total, item) => total + item.price * item.qty,
75 0
76 );
77}
78
79export function sortByPrice(items, ascending) {
80
81 if(!Array.isArray(items)){
82 return []
83 }
84
85 return [...items].sort((a, b) =>
86 ascending ? a.price - b.price : b.price - a.price
87 );
88}
89
90export function formatBill(items) {
91
92 if(!Array.isArray(items) || items.length == 0){
93 return ""
94 }
95
96 return items
97 .map(item => `${item.name} x ${item.qty} = Rs.${item.price * item.qty}`)
98 .join("\n");
99
100}
101