-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbanking.js
More file actions
46 lines (44 loc) · 988 Bytes
/
banking.js
File metadata and controls
46 lines (44 loc) · 988 Bytes
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
function makeBank() {
function makeAccount(number) {
let balance = 0;
let transactions = [];
return {
deposit(value) {
balance += value;
transactions.push({type: "deposit", amount: value,})
return value;
},
withdraw(value) {
if (balance < value) {
value = balance;
}
balance -= value;
transactions.push({type: "withdraw", amount: value,})
return value;
},
balance() {
return balance;
},
number() {
return number;
},
transactions() {
return transactions;
},
};
}
let accounts = [];
return {
openAccount() {
let number = accounts.length + 101
let account = makeAccount(number);
accounts.push(account);
return account;
},
transfer(source, destination, amount) {
source.withdraw(amount);
destination.deposit(amount);
return amount;
},
}
}