-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (82 loc) · 1.73 KB
/
index.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
function getMoneyChange(money) {
//your code here\\
let copyMoney = money
let totalStocks = 0
let arr = Object.entries(moneyStocks)
let i = arr.length - 1
while(i >= 0 ){
let pecahan = arr[i][0]
let lembar = arr[i][1]
//console.log(lembar,pecahan, copyMoney, i)
if(lembar === 0){
i--
}else{
if(copyMoney > 0){
if(pecahan * lembar <= copyMoney){
copyMoney -= pecahan * lembar
i--
}else{
arr[i][1]--
}
}else{
arr[i][1]--
}
}
}
//console.log(copyMoney)
if(copyMoney !== 0){
console.log('Maaf uang kembalian tidak cukup')
return {}
}
for(let i = arr.length - 1 ; i >= 0 ; i--){
let arrIndex = arr[i]
arrIndex[1] > 0 ? console.log(`${arrIndex[0]} ${arrIndex[1]} lembar`) : {}
moneyStocks[arrIndex[0]] -= arrIndex[1]
}
}
const moneyStocks = {
100000: 1,
50000: 2,
20000: 4,
10000: 5,
5000: 5,
1000: 10,
500: 5
}
getMoneyChange(75000)
/*
50000 1 lembar
20000 1 lembar
5000 1 lembar
*/
getMoneyChange(190000)
/*
100000 1 lembar
50000 1 lembar
20000 2 lembar
*/
getMoneyChange(190000)
/*
Maaf uang kembalian tidak cukup
*/
getMoneyChange(100000)
/*
20000 1 lembar
10000 5 lembar
5000 4 lembar
1000 10 lembar
*/
getMoneyChange(400)
/*
Maaf uang kembalian tidak cukup
*/
console.log(moneyStocks)
/*
{ '500': 5,
'1000': 0,
'5000': 0,
'10000': 0,
'20000': 0,
'50000': 0,
'100000': 0 }
*/