Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 90f1daa

Browse files
committed
week4 prepexercise is done
1 parent 11b76aa commit 90f1daa

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

Week4/prep-exercises/1-wallet/ex1-closure-example.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import eurosFormatter from "./euroFormatter.js";
1+
import eurosFormatter from './euroFormatter.js';
22

33
/**
44
* This is the closure way of doing things and we have already completed it for you so you don't need to do anything.
@@ -68,9 +68,9 @@ function createWallet(name, cash = 0) {
6868
}
6969

7070
function main() {
71-
const walletJack = createWallet("Jack", 100);
72-
const walletJoe = createWallet("Joe", 10);
73-
const walletJane = createWallet("Jane", 20);
71+
const walletJack = createWallet('Jack', 100);
72+
const walletJoe = createWallet('Joe', 10);
73+
const walletJane = createWallet('Jane', 20);
7474

7575
walletJack.transferInto(walletJoe, 50);
7676
walletJack.setDailyAllowance(80);

Week4/prep-exercises/1-wallet/ex2-classes.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Wallet {
77
constructor(name, cash) {
88
this.#name = name;
99
this.#cash = cash;
10+
this.dailyAllowance = 40;
1011
}
1112

1213
get name() {
@@ -23,6 +24,12 @@ class Wallet {
2324
return 0;
2425
}
2526

27+
// first check if the amount to withdraw is greater than the daily allowance
28+
if (amount > this.dailyAllowance) {
29+
console.log(`Insufficient remaining daily allowance!`);
30+
return 0;
31+
}
32+
2633
this.#cash -= amount;
2734
return amount;
2835
}
@@ -33,10 +40,19 @@ class Wallet {
3340
wallet.name
3441
}`
3542
);
43+
3644
const withdrawnAmount = this.withdraw(amount);
3745
wallet.deposit(withdrawnAmount);
3846
}
3947

48+
// setting daily allowance to 80
49+
setDailyAllowance(newAllowance) {
50+
this.dailyAllowance = newAllowance;
51+
console.log(
52+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
53+
);
54+
}
55+
4056
reportBalance() {
4157
console.log(
4258
`Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}`
@@ -49,9 +65,10 @@ function main() {
4965
const walletJoe = new Wallet('Joe', 10);
5066
const walletJane = new Wallet('Jane', 20);
5167

68+
walletJack.transferInto(walletJoe, 50);
69+
walletJack.setDailyAllowance(80);
5270
walletJack.transferInto(walletJoe, 50);
5371
walletJane.transferInto(walletJoe, 25);
54-
5572
walletJane.deposit(20);
5673
walletJane.transferInto(walletJoe, 25);
5774

Week4/prep-exercises/1-wallet/ex3-object.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ function createWallet(name, cash = 0) {
44
return {
55
_name: name,
66
_cash: cash,
7+
_dailyAllowance: 40, // default value for daily allowance
78

89
deposit: function (amount) {
910
this._cash += amount;
@@ -15,10 +16,24 @@ function createWallet(name, cash = 0) {
1516
return 0;
1617
}
1718

19+
// first check if the amount to withdraw is greater than the daily allowance
20+
if (amount > this._dailyAllowance) {
21+
console.log(`Insufficient remaining daily allowance!`);
22+
return 0;
23+
}
24+
1825
this._cash -= amount;
1926
return amount;
2027
},
2128

29+
// setting daily allowance to 80
30+
setDailyAllowance: function (newAllowance) {
31+
this._dailyAllowance = newAllowance;
32+
console.log(
33+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
34+
);
35+
},
36+
2237
transferInto: function (wallet, amount) {
2338
console.log(
2439
`Transferring ${eurosFormatter.format(amount)} from ${
@@ -46,6 +61,8 @@ function main() {
4661
const walletJoe = createWallet('Joe', 10);
4762
const walletJane = createWallet('Jane', 20);
4863

64+
walletJack.transferInto(walletJoe, 50);
65+
walletJack.setDailyAllowance(80);
4966
walletJack.transferInto(walletJoe, 50);
5067
walletJane.transferInto(walletJoe, 25);
5168

Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,21 @@ function withdraw(amount) {
1010
return 0;
1111
}
1212

13+
// first check if the amount to withdraw is greater than the daily allowance
14+
if (amount > this._dailyAllowance) {
15+
console.log(`Insufficient remaining daily allowance!`);
16+
return 0;
17+
}
18+
1319
this._cash -= amount;
1420
return amount;
1521
}
1622

23+
function setDailyAllowance(newAllowance) {
24+
this._dailyAllowance = newAllowance;
25+
console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`);
26+
}
27+
1728
function transferInto(wallet, amount) {
1829
console.log(
1930
`Transferring ${eurosFormatter.format(amount)} from ${
@@ -38,11 +49,13 @@ function createWallet(name, cash = 0) {
3849
return {
3950
_name: name,
4051
_cash: cash,
52+
_dailyAllowance: 40, // default value for daily allowance
4153
deposit,
4254
withdraw,
4355
transferInto,
4456
reportBalance,
4557
getName,
58+
setDailyAllowance,
4659
};
4760
}
4861

@@ -51,6 +64,8 @@ function main() {
5164
const walletJoe = createWallet('Joe', 10);
5265
const walletJane = createWallet('Jane', 20);
5366

67+
walletJack.transferInto(walletJoe, 50);
68+
walletJack.setDailyAllowance(80);
5469
walletJack.transferInto(walletJoe, 50);
5570
walletJane.transferInto(walletJoe, 25);
5671

Week4/prep-exercises/1-wallet/ex5-prototype.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import eurosFormatter from './euroFormatter.js';
33
function Wallet(name, cash) {
44
this._name = name;
55
this._cash = cash;
6+
this._dailyAllowance = 40; // default value for daily allowance
67
}
78

89
Wallet.prototype.deposit = function (amount) {
@@ -14,11 +15,21 @@ Wallet.prototype.withdraw = function (amount) {
1415
console.log(`Insufficient funds!`);
1516
return 0;
1617
}
18+
// first check if the amount to withdraw is greater than the daily allowance
19+
if (amount > this._dailyAllowance) {
20+
console.log(`Insufficient remaining daily allowance!`);
21+
return 0;
22+
}
1723

1824
this._cash -= amount;
1925
return amount;
2026
};
2127

28+
Wallet.prototype.setDailyAllowance = function (newAllowance) {
29+
this._dailyAllowance = newAllowance;
30+
console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`);
31+
};
32+
2233
Wallet.prototype.transferInto = function (wallet, amount) {
2334
console.log(
2435
`Transferring ${eurosFormatter.format(amount)} from ${
@@ -44,6 +55,8 @@ function main() {
4455
const walletJoe = new Wallet('Joe', 10);
4556
const walletJane = new Wallet('Jane', 20);
4657

58+
walletJack.transferInto(walletJoe, 50);
59+
walletJack.setDailyAllowance(80);
4760
walletJack.transferInto(walletJoe, 50);
4861
walletJane.transferInto(walletJoe, 25);
4962

0 commit comments

Comments
 (0)