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

Skip to content

Commit 38817cd

Browse files
committed
finished prep exercices
1 parent 6a4274a commit 38817cd

File tree

5 files changed

+63
-19
lines changed

5 files changed

+63
-19
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use strict";
12
import eurosFormatter from "./euroFormatter.js";
23

34
/**

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
"use strict";
12
import eurosFormatter from "./euroFormatter.js";
23

34
class Wallet {
45
#name;
56
#cash;
6-
dailyAllowance = 40;
7-
dayTotalWithdrawals = 0;
7+
#dailyAllowance = 40;
8+
#dayTotalWithdrawals = 0;
89

910
constructor(name, cash) {
1011
this.#name = name;
@@ -25,13 +26,13 @@ class Wallet {
2526
return 0;
2627
}
2728

28-
if (this.dayTotalWithdrawals + amount > this.dailyAllowance) {
29+
if (this.#dayTotalWithdrawals + amount > this.#dailyAllowance) {
2930
console.log(`Insufficient remaining daily allowance!`);
3031
return 0;
3132
}
3233

3334
this.#cash -= amount;
34-
this.dayTotalWithdrawals += amount;
35+
this.#dayTotalWithdrawals += amount;
3536
return amount;
3637
}
3738

@@ -45,14 +46,14 @@ class Wallet {
4546
wallet.deposit(withdrawnAmount);
4647
}
4748
setDailyAllowance(newAllowance) {
48-
this.dailyAllowance = newAllowance;
49+
this.#dailyAllowance = newAllowance;
4950
console.log(
5051
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
5152
);
5253
}
5354

5455
resetDailyAllowance() {
55-
this.dayTotalWithdrawals = 0;
56+
this.#dayTotalWithdrawals = 0;
5657
}
5758

5859
reportBalance() {
@@ -64,6 +65,7 @@ class Wallet {
6465

6566
function main() {
6667
const walletJack = new Wallet("Jack", 100);
68+
console.log(typeof walletJack);
6769
const walletJoe = new Wallet("Joe", 10);
6870
const walletJane = new Wallet("Jane", 20);
6971

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
"use strict";
12
import eurosFormatter from "./euroFormatter.js";
23

34
function createWallet(name, cash = 0) {
45
return {
56
_name: name,
67
_cash: cash,
7-
dailyAllowance: 40,
8-
dayTotalWithdrawals: 0,
8+
_dailyAllowance: 40,
9+
_dayTotalWithdrawals: 0,
910

1011
deposit: function (amount) {
1112
this._cash += amount;
@@ -16,7 +17,7 @@ function createWallet(name, cash = 0) {
1617
console.log(`Insufficient funds!`);
1718
return 0;
1819
}
19-
if (this.dayTotalWithdrawals + amount > this.dailyAllowance) {
20+
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
2021
console.log(`Insufficient remaining daily allowance!`);
2122
return 0;
2223
}
@@ -36,14 +37,14 @@ function createWallet(name, cash = 0) {
3637
},
3738

3839
setDailyAllowance: function (newAllowance) {
39-
this.dailyAllowance = newAllowance;
40+
this._dailyAllowance = newAllowance;
4041
console.log(
4142
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
4243
);
4344
},
4445

4546
resetDailyAllowance: function () {
46-
this.dayTotalWithdrawals = 0;
47+
this._dayTotalWithdrawals = 0;
4748
},
4849

4950
reportBalance: function () {

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import eurosFormatter from './euroFormatter.js';
1+
"use strict";
2+
import eurosFormatter from "./euroFormatter.js";
23

34
function deposit(amount) {
45
this._cash += amount;
@@ -9,8 +10,13 @@ function withdraw(amount) {
910
console.log(`Insufficient funds!`);
1011
return 0;
1112
}
13+
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
14+
console.log(`Insufficient remaining daily allowance!`);
15+
return 0;
16+
}
1217

1318
this._cash -= amount;
19+
this._dayTotalWithdrawals += amount;
1420
return amount;
1521
}
1622

@@ -34,10 +40,22 @@ function getName() {
3440
return this._name;
3541
}
3642

43+
function resetDailyAllowance() {
44+
this._dayTotalWithdrawals = 0;
45+
}
46+
47+
function setDailyAllowance(amount) {
48+
this._dailyAllowance = amount;
49+
}
50+
3751
function createWallet(name, cash = 0) {
3852
return {
3953
_name: name,
4054
_cash: cash,
55+
_dailyAllowance: 40,
56+
_dayTotalWithdrawals: 0,
57+
resetDailyAllowance,
58+
setDailyAllowance,
4159
deposit,
4260
withdraw,
4361
transferInto,
@@ -47,11 +65,14 @@ function createWallet(name, cash = 0) {
4765
}
4866

4967
function main() {
50-
const walletJack = createWallet('Jack', 100);
51-
const walletJoe = createWallet('Joe', 10);
52-
const walletJane = createWallet('Jane', 20);
68+
const walletJack = createWallet("Jack", 100);
69+
const walletJoe = createWallet("Joe", 10);
70+
const walletJane = createWallet("Jane", 20);
5371

5472
walletJack.transferInto(walletJoe, 50);
73+
walletJack.setDailyAllowance(80);
74+
walletJack.transferInto(walletJoe, 50);
75+
5576
walletJane.transferInto(walletJoe, 25);
5677

5778
walletJane.deposit(20);

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import eurosFormatter from './euroFormatter.js';
1+
"use strict";
2+
import eurosFormatter from "./euroFormatter.js";
23

34
function Wallet(name, cash) {
45
this._name = name;
56
this._cash = cash;
7+
this._dailyAllowance = 40;
8+
this._dayTotalWithdrawals = 0;
69
}
710

811
Wallet.prototype.deposit = function (amount) {
@@ -14,8 +17,13 @@ Wallet.prototype.withdraw = function (amount) {
1417
console.log(`Insufficient funds!`);
1518
return 0;
1619
}
20+
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
21+
console.log(`Insufficient remaining daily allowance!`);
22+
return 0;
23+
}
1724

1825
this._cash -= amount;
26+
this._dayTotalWithdrawals += amount;
1927
return amount;
2028
};
2129

@@ -29,6 +37,14 @@ Wallet.prototype.transferInto = function (wallet, amount) {
2937
wallet.deposit(withdrawnAmount);
3038
};
3139

40+
Wallet.prototype.setDailyAllowance = function (amount) {
41+
this._dailyAllowance = amount;
42+
};
43+
44+
Wallet.prototype.resetDailyWithdrawals = function () {
45+
this._dayTotalWithdrawals = 0;
46+
};
47+
3248
Wallet.prototype.reportBalance = function () {
3349
console.log(
3450
`Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`
@@ -40,11 +56,14 @@ Wallet.prototype.getName = function () {
4056
};
4157

4258
function main() {
43-
const walletJack = new Wallet('Jack', 100);
44-
const walletJoe = new Wallet('Joe', 10);
45-
const walletJane = new Wallet('Jane', 20);
59+
const walletJack = new Wallet("Jack", 100);
60+
const walletJoe = new Wallet("Joe", 10);
61+
const walletJane = new Wallet("Jane", 20);
4662

4763
walletJack.transferInto(walletJoe, 50);
64+
walletJack.setDailyAllowance(80);
65+
walletJack.transferInto(walletJoe, 50);
66+
4867
walletJane.transferInto(walletJoe, 25);
4968

5069
walletJane.deposit(20);

0 commit comments

Comments
 (0)