1
1
export default function ATM ( ) {
2
- const moneyStore = new Map ( [
2
+ let moneyStore = new Map ( [
3
3
[ 20 , 0 ] ,
4
4
[ 50 , 0 ] ,
5
5
[ 100 , 0 ] ,
@@ -9,17 +9,17 @@ export default function ATM() {
9
9
const indexToMoney = [ 20 , 50 , 100 , 200 , 500 ] ;
10
10
return {
11
11
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 ] ;
13
15
const key = indexToMoney [ index ] ;
14
- if ( ! moneyStore . has ( key ) ) {
15
- throw new Error ( "moneyStore Not Found" ) ;
16
- }
16
+
17
17
moneyStore . set ( key , ( moneyStore . get ( key ) ?? 0 ) + count ) ;
18
18
}
19
19
} ,
20
20
21
21
withdraw ( amount : number ) : number [ ] {
22
- const delta = Object . fromEntries ( [
22
+ const delta = new Map ( [
23
23
[ 20 , 0 ] ,
24
24
[ 50 , 0 ] ,
25
25
[ 100 , 0 ] ,
@@ -34,7 +34,7 @@ export default function ATM() {
34
34
changedStore . get ( money ) ?? 0 ,
35
35
) ;
36
36
amount -= money * d ;
37
- delta [ money ] += d ;
37
+ delta . set ( money , + d + ( delta . get ( money ) ?? 0 ) ) ;
38
38
changedStore . set (
39
39
money ,
40
40
- d + ( changedStore . get ( money ) ?? 0 ) ,
@@ -44,10 +44,9 @@ export default function ATM() {
44
44
if ( amount > 0 ) {
45
45
return [ - 1 ] ;
46
46
} 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 ( ) ) ;
51
50
}
52
51
} ,
53
52
} ;
0 commit comments