forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathex5-wallet.js
More file actions
70 lines (56 loc) · 1.48 KB
/
ex5-wallet.js
File metadata and controls
70 lines (56 loc) · 1.48 KB
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
const eurosFormatter = new Intl.NumberFormat('nl-NL', {
style: 'currency',
currency: 'EUR',
});
function createWallet(name, cash = 0) {
function deposit(amount) {
cash += amount;
}
function withdraw(amount) {
if (cash - amount < 0) {
console.log(`Insufficient funds!`);
return 0;
}
cash -= amount;
return amount;
}
function transferInto(wallet, amount) {
console.log(
`Transferring ${eurosFormatter.format(amount)} from ${name} to ${wallet.getName()}`
);
const withdrawnAmount = withdraw(amount);
wallet.deposit(withdrawnAmount);
}
function reportBalance() {
console.log(`Name: ${name}, balance: ${eurosFormatter.format(cash)}`);
}
const getName = () => name;
return {
deposit,
withdraw,
transferInto,
reportBalance,
getName,
};
}
const walletJack = createWallet('Jack', 100);
const walletJoe = createWallet('Joe', 10);
const walletJane = createWallet('Jane', 20);
walletJack.transferInto(walletJoe, 50);
walletJane.transferInto(walletJoe, 25);
walletJane.deposit(20);
walletJane.transferInto(walletJoe, 25);
walletJack.reportBalance();
walletJoe.reportBalance();
walletJane.reportBalance();
// * End of exercise code
/*******************************************************************************/
// prettier-ignore
// eslint-disable-next-line no-unused-vars
const quiz = {
q1: { answer: 'b' },
q2: { answer: 'c' },
q3: { answer: 'a' },
q4: { answer: 'b' },
q5: { answer: 'c' },
};