Skip to content

Commit f6c7d52

Browse files
committed
Update index.ts
1 parent 19a05e4 commit f6c7d52

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

design-an-atm-machine/index.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default function ATM() {
2-
const moneyStore = new Map([
2+
let moneyStore = new Map([
33
[20, 0],
44
[50, 0],
55
[100, 0],
@@ -9,17 +9,17 @@ export default function ATM() {
99
const indexToMoney = [20, 50, 100, 200, 500];
1010
return {
1111
deposit(banknotesCount: number[]): void {
12-
for (const [index, count] of banknotesCount.entries()) {
12+
for (let i = 0; i < 5; i++) {
13+
const index = i;
14+
const count = banknotesCount[i];
1315
const key = indexToMoney[index];
14-
if (!moneyStore.has(key)) {
15-
throw new Error("moneyStore Not Found");
16-
}
16+
1717
moneyStore.set(key, (moneyStore.get(key) ?? 0) + count);
1818
}
1919
},
2020

2121
withdraw(amount: number): number[] {
22-
const delta = Object.fromEntries([
22+
const delta = new Map([
2323
[20, 0],
2424
[50, 0],
2525
[100, 0],
@@ -34,7 +34,7 @@ export default function ATM() {
3434
changedStore.get(money) ?? 0,
3535
);
3636
amount -= money * d;
37-
delta[money] += d;
37+
delta.set(money, +d + (delta.get(money) ?? 0));
3838
changedStore.set(
3939
money,
4040
-d + (changedStore.get(money) ?? 0),
@@ -44,10 +44,9 @@ export default function ATM() {
4444
if (amount > 0) {
4545
return [-1];
4646
} else {
47-
changedStore.forEach((value, key) =>
48-
moneyStore.set(key, value)
49-
);
50-
return Object.values(delta);
47+
moneyStore = changedStore;
48+
49+
return Array.from(delta.values());
5150
}
5251
},
5352
};

0 commit comments

Comments
 (0)