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

Skip to content

Commit 5e8f40e

Browse files
prepex-w4
1 parent c4a01c2 commit 5e8f40e

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"liveServer.settings.port": 5501
3+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,27 @@ import eurosFormatter from './euroFormatter.js';
33
class Wallet {
44
#name;
55
#cash;
6+
#dailyAllowance;
7+
#dayTotalWithdrawals;
68

79
constructor(name, cash) {
810
this.#name = name;
911
this.#cash = cash;
12+
this.#dailyAllowance = 40;
13+
this.#dayTotalWithdrawals = 0;
14+
}
15+
16+
resetDailyAllowance() {
17+
this.#dayTotalWithdrawals = 0;
18+
console.log(this.#dayTotalWithdrawals);
19+
}
20+
21+
get getDailyAllowance() {
22+
return this.#dailyAllowance;
23+
}
24+
25+
setDailyAllowance(newAllowance) {
26+
this.#dailyAllowance = newAllowance;
1027
}
1128

1229
get name() {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@ function createWallet(name, cash = 0) {
44
return {
55
_name: name,
66
_cash: cash,
7+
_dailyAllowance: 40,
8+
_dayTotalWithdrawals: 0,
79

810
deposit: function (amount) {
911
this._cash += amount;
1012
},
1113

14+
resetDailyAllowance() {
15+
return (this._dayTotalWithdrawals = 0);
16+
},
17+
18+
setDailyAllowance(newAllowance) {
19+
this._dayTotalWithdrawals = newAllowance;
20+
},
21+
1222
withdraw: function (amount) {
1323
if (this._cash - amount < 0) {
1424
console.log(`Insufficient funds!`);

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,23 @@ function withdraw(amount) {
1010
return 0;
1111
}
1212

13+
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
14+
return 0;
15+
}
16+
1317
this._cash -= amount;
18+
this._dayTotalWithdrawals += amount;
1419
return amount;
1520
}
1621

22+
function resetDailyAllowance() {
23+
return (dayTotalWithdrawals = 0);
24+
}
25+
26+
function setDailyAllowance(newAllowance) {
27+
this.dayAllowance = newAllowance;
28+
}
29+
1730
function transferInto(wallet, amount) {
1831
console.log(
1932
`Transferring ${eurosFormatter.format(amount)} from ${
@@ -43,6 +56,8 @@ function createWallet(name, cash = 0) {
4356
transferInto,
4457
reportBalance,
4558
getName,
59+
reportBalance,
60+
setDailyAllowance,
4661
};
4762
}
4863

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import eurosFormatter from './euroFormatter.js';
33
function Wallet(name, cash) {
44
this._name = name;
55
this._cash = cash;
6+
this._dailyAllowance = 40;
7+
this._dayTotalWithdrawals = 0;
68
}
79

810
Wallet.prototype.deposit = function (amount) {
@@ -39,6 +41,14 @@ Wallet.prototype.getName = function () {
3941
return this._name;
4042
};
4143

44+
Wallet.prototype.resetDailyAllowance = function resetDailyAllowance() {
45+
return (_dayTotalWithdrawals = 0);
46+
};
47+
48+
Wallet.prototype.setDailyAllowance(newAllowance) = function setDailyAllowance(newAllowance) {
49+
return this._dailyAllowance = newAllowance;
50+
};
51+
4252
function main() {
4353
const walletJack = new Wallet('Jack', 100);
4454
const walletJoe = new Wallet('Joe', 10);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<script src="./ex3-object.js" type="module"></script>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)