-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtokoX.js
99 lines (88 loc) · 4.02 KB
/
tokoX.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Toko X yang sedang melakukan SALE ingin menghitung jumlah profit untuk setiap jenis barang yang terjual pada hari itu.
Barang-barang SALE yang akan dihitung penjualannya:
Sepatu brand Stacattu seharga 1500000 dan stock barang yang tesedia 10
Baju brand Zoro seharga 500000 dan stock barang yang tesedia 2
Sweater brand Uniklooh seharga 175000 dan stock barang yang tersedia 1
Function akan menerima array yang berisikan object pembeli (nama pembeli, barang yang ingin dibeli dan jumlah barang yang dibelinya). Jika stock barang kurang dari jumlah yang ingin dibeli oleh pembeli maka pembeli batal untuk membeli barang tersebut.
Function countProfit akan mengembalikan/me-return sebuah array of object dimana array tersebut berisi objek-objek barang dari toko X tersebut yang berisikan info nama barang, siapa saja yang membeli, sisa stock barang dan total pemasukan untuk barang tersebut
*/
function countProfit(shoppers) {
let listBarang = [ ['Sepatu Stacattu', 1500000, 10],
['Baju Zoro', 500000, 2],
['Sweater Uniklooh', 175000, 1]
];
if (shoppers.length === 0){
return shoppers
}
var names1 = [];
var names2 = [];
var names3 = [];
var profit1 = 0;
var profit2 = 0;
var profit3 = 0;
for (var i in shoppers){
if (shoppers[i].product === listBarang[0][0]){
if (listBarang[0][2] >= shoppers[i].amount){
names1.push(shoppers[i].name)
listBarang[0].splice(2, 1, (listBarang[0][2]- shoppers[i].amount))
profit1 += listBarang[0][1] * shoppers[i].amount
}
} else if (shoppers[i].product === listBarang[1][0]){
if (listBarang[1][2] >= shoppers[i].amount){
names2.push(shoppers[i].name)
listBarang[1].splice(2, 1, (listBarang[1][2]- shoppers[i].amount))
profit2 += listBarang[1][1] * shoppers[i].amount
}
} else if (shoppers[i].product === listBarang[2][0]){
if (listBarang[2][2] >= shoppers[i].amount){
names3.push(shoppers[i].name)
listBarang[2].splice(2, 1, (listBarang[2][2]- shoppers[i].amount))
profit3 += listBarang[2][1] * shoppers[i].amount
}
}
}
var list = [{product: listBarang[0][0], shoppers: names1, leftOver: listBarang[0][2], totalProfit: profit1}, {product: listBarang[1][0], shoppers: names2, leftOver: listBarang[1][2], totalProfit: profit2}, {product: listBarang[2][0], shoppers: names3, leftOver: listBarang[2][2], totalProfit: profit3}]
return list
}
// TEST CASES
console.log(countProfit([{name: 'Windi', product: 'Sepatu Stacattu', amount: 2}, {name: 'Vanessa', product: 'Sepatu Stacattu', amount: 3}, {name: 'Rani', product: 'Sweater Uniklooh', amount: 2}]));
//[ { product: 'Sepatu Stacattu',
// shoppers: [ 'Windi', 'Vanessa' ],
// leftOver: 5,
// totalProfit: 7500000 },
// { product: 'Baju Zoro',
// shoppers: [],
// leftOver: 2,
// totalProfit: 0 },
// { product: 'Sweater Uniklooh',
// shoppers: [],
// leftOver: 1,
// totalProfit: 0 } ]
console.log(countProfit([{name: 'Windi', product: 'Sepatu Stacattu', amount: 8}, {name: 'Vanessa', product: 'Sepatu Stacattu', amount: 10}, {name: 'Rani', product: 'Sweater Uniklooh', amount: 1}, {name: 'Devi', product: 'Baju Zoro', amount: 1}, {name: 'Lisa', product: 'Baju Zoro', amount: 1}]));
// [ { product: 'Sepatu Stacattu',
// shoppers: [ 'Windi' ],
// leftOver: 2,
// totalProfit: 12000000 },
// { product: 'Baju Zoro',
// shoppers: [ 'Devi', 'Lisa' ],
// leftOver: 0,
// totalProfit: 1000000 },
// { product: 'Sweater Uniklooh',
// shoppers: [ 'Rani' ],
// leftOver: 0,
// totalProfit: 175000 } ]
console.log(countProfit([{name: 'Windi', product: 'Sepatu Naiki', amount: 5}]));
// [ { product: 'Sepatu Stacattu',
// shoppers: [],
// leftOver: 10,
// totalProfit: 0 },
// { product: 'Baju Zoro',
// shoppers: [],
// leftOver: 2,
// totalProfit: 0 },
// { product: 'Sweater Uniklooh',
// shoppers: [],
// leftOver: 1,
// totalProfit: 0 } ]
console.log(countProfit([])); //[]