-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise-12.js
81 lines (72 loc) · 2.52 KB
/
exercise-12.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
function countProfit(arr) {
let listBarang = [['Sepatu Stacattu', 1500000, 10],
['Baju Zoro', 500000, 2],
['Sweater Uniklooh', 175000, 1]
];
let hasil = []
if (arr.length === 0) {
return hasil
}
else {
for (let i = 0; i < listBarang.length; i++) {
let product = {
product: listBarang[i][0],
shoppers: [],
leftOver: listBarang[i][2],
totalProfit: 0
}
for (let y = 0; y < arr.length; y++) {
if (arr[y].product === product.product) {
if (product.leftOver >= arr[y].amount) {
product.shoppers.push(arr[y].name)
product.leftOver -= arr[y].amount
product.totalProfit += listBarang[i][1] * arr[y].amount
}
}
}
hasil.push(product)
}
}
return hasil
}
// 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([])); //[]