forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (102 loc) · 2.76 KB
/
index.js
File metadata and controls
116 lines (102 loc) · 2.76 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Based on an example from: Philipp Beau (@ze_german)
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
/*******************************************************************************
* TODO: Multiple choice: provide your answers by replacing `undefined` with the
* TODO: letter corresponding to your choice, e.g. answer: 'a'
******************************************************************************/
// prettier-ignore
// eslint-disable-next-line no-unused-vars
const quiz = {
q1: {
question: 'At line 24, which variables are in the scope marked Closure?',
choices: {
a: 'There is no scope marked Closure',
b: 'cash, name',
c: 'amount, this, wallet'
},
answer: 'b',
},
q2: {
question: 'What is in the Call Stack, from top to bottom?',
choices: {
a: 'withdraw, anonymous',
b: 'anonymous, transferInto',
c: 'transferInto, anonymous'
},
answer: 'c',
},
q3: {
question: 'What tooltip appears when hovering over the third debug button?',
choices: {
a: 'Step into next function call',
b: 'Step out of current function',
c: 'Step'
},
answer: 'a',
},
q4: {
question: 'What is displayed in the console?',
choices: {
a: 'Transferring € 50,00 from Jack to Joe',
b: 'Transferring € 50,00 from Jack to undefined',
c: 'Transferring € 50,00 from Jack to Jane'
},
answer: 'a',
},
q5: {
question: 'The owner of the wallet with insufficient funds is:',
choices: {
a: 'Jack',
b: 'Joe',
c: 'Jane'
},
answer: 'c',
},
};