Skip to content

Commit fe37a6a

Browse files
committed
Vlad-Nachikov-w4-prep
1 parent 4f263fb commit fe37a6a

File tree

5 files changed

+127
-34
lines changed

5 files changed

+127
-34
lines changed

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

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

33
class Wallet {
44
#name;
55
#cash;
6+
#dailyAllowance;
7+
#dayTotalWithdrawals;
68

7-
constructor(name, cash) {
9+
constructor(name, cash = 0) {
810
this.#name = name;
911
this.#cash = cash;
12+
this.#dailyAllowance = 40;
13+
this.#dayTotalWithdrawals = 0;
1014
}
1115

1216
get name() {
@@ -18,12 +22,18 @@ class Wallet {
1822
}
1923

2024
withdraw(amount) {
21-
if (this.#cash - amount < 0) {
25+
if (this.#cash < amount) {
2226
console.log(`Insufficient funds!`);
2327
return 0;
2428
}
2529

30+
if (this.#dayTotalWithdrawals + amount > this.#dailyAllowance) {
31+
console.log(`Insufficient remaining daily allowance!`);
32+
return 0;
33+
}
34+
2635
this.#cash -= amount;
36+
this.#dayTotalWithdrawals += amount;
2737
return amount;
2838
}
2939

@@ -33,8 +43,22 @@ class Wallet {
3343
wallet.name
3444
}`
3545
);
46+
3647
const withdrawnAmount = this.withdraw(amount);
37-
wallet.deposit(withdrawnAmount);
48+
if (withdrawnAmount > 0) {
49+
wallet.deposit(withdrawnAmount);
50+
}
51+
}
52+
53+
setDailyAllowance(newAllowance) {
54+
this.#dailyAllowance = newAllowance;
55+
console.log(
56+
`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`
57+
);
58+
}
59+
60+
resetDailyAllowance() {
61+
this.#dayTotalWithdrawals = 0;
3862
}
3963

4064
reportBalance() {
@@ -45,9 +69,9 @@ class Wallet {
4569
}
4670

4771
function main() {
48-
const walletJack = new Wallet('Jack', 100);
49-
const walletJoe = new Wallet('Joe', 10);
50-
const walletJane = new Wallet('Jane', 20);
72+
const walletJack = new Wallet("Jack", 100);
73+
const walletJoe = new Wallet("Joe", 10);
74+
const walletJane = new Wallet("Jane", 20);
5175

5276
walletJack.transferInto(walletJoe, 50);
5377
walletJane.transferInto(walletJoe, 25);
@@ -58,6 +82,12 @@ function main() {
5882
walletJack.reportBalance();
5983
walletJoe.reportBalance();
6084
walletJane.reportBalance();
85+
86+
walletJane.setDailyAllowance(100);
87+
walletJane.resetDailyAllowance();
88+
walletJane.transferInto(walletJoe, 50);
89+
90+
walletJane.reportBalance();
6191
}
6292

6393
main();
Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1-
import eurosFormatter from './euroFormatter.js';
1+
import eurosFormatter from "./euroFormatter.js";
22

33
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

1214
withdraw: function (amount) {
15+
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
16+
console.log(`Daily allowance exceeded!`);
17+
return 0;
18+
}
19+
1320
if (this._cash - amount < 0) {
1421
console.log(`Insufficient funds!`);
1522
return 0;
1623
}
1724

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

@@ -26,7 +34,9 @@ function createWallet(name, cash = 0) {
2634
} to ${wallet.getName()}`
2735
);
2836
const withdrawnAmount = this.withdraw(amount);
29-
wallet.deposit(withdrawnAmount);
37+
if (withdrawnAmount > 0) {
38+
wallet.deposit(withdrawnAmount);
39+
}
3040
},
3141

3242
reportBalance: function () {
@@ -38,23 +48,37 @@ function createWallet(name, cash = 0) {
3848
getName: function () {
3949
return this._name;
4050
},
51+
52+
resetDailyAllowance: function () {
53+
this._dayTotalWithdrawals = 0;
54+
},
55+
56+
setDailyAllowance: function (newAllowance) {
57+
this._dailyAllowance = newAllowance;
58+
},
4159
};
4260
}
4361

4462
function main() {
45-
const walletJack = createWallet('Jack', 100);
46-
const walletJoe = createWallet('Joe', 10);
47-
const walletJane = createWallet('Jane', 20);
63+
const walletJack = createWallet("Jack", 100);
64+
const walletJoe = createWallet("Joe", 10);
65+
const walletJane = createWallet("Jane", 20);
4866

4967
walletJack.transferInto(walletJoe, 50);
50-
walletJane.transferInto(walletJoe, 25);
68+
walletJane.transferInto(walletJoe, 25); // Exceeds limit (default = 40)
5169

5270
walletJane.deposit(20);
53-
walletJane.transferInto(walletJoe, 25);
71+
walletJane.transferInto(walletJoe, 25); // OK if still within daily limit
5472

5573
walletJack.reportBalance();
5674
walletJoe.reportBalance();
5775
walletJane.reportBalance();
76+
77+
walletJane.setDailyAllowance(100);
78+
walletJane.resetDailyAllowance();
79+
walletJane.transferInto(walletJoe, 50); // Now works after reset + increased limit
80+
81+
walletJane.reportBalance();
5882
}
5983

6084
main();
Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
import eurosFormatter from './euroFormatter.js';
1+
import eurosFormatter from "./euroFormatter.js";
22

33
function deposit(amount) {
44
this._cash += amount;
55
}
66

77
function withdraw(amount) {
8+
if (this._dayTotalWithdrawals + amount > this._dailyAllowance) {
9+
console.log(`Daily allowance exceeded!`);
10+
return 0;
11+
}
12+
813
if (this._cash - amount < 0) {
914
console.log(`Insufficient funds!`);
1015
return 0;
1116
}
1217

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

@@ -20,8 +26,11 @@ function transferInto(wallet, amount) {
2026
this._name
2127
} to ${wallet.getName()}`
2228
);
29+
2330
const withdrawnAmount = this.withdraw(amount);
24-
wallet.deposit(withdrawnAmount);
31+
if (withdrawnAmount > 0) {
32+
wallet.deposit(withdrawnAmount);
33+
}
2534
}
2635

2736
function reportBalance() {
@@ -34,32 +43,51 @@ function getName() {
3443
return this._name;
3544
}
3645

46+
function resetDailyAllowance() {
47+
this._dayTotalWithdrawals = 0;
48+
}
49+
50+
function setDailyAllowance(newAllowance) {
51+
this._dailyAllowance = newAllowance;
52+
}
53+
3754
function createWallet(name, cash = 0) {
3855
return {
3956
_name: name,
4057
_cash: cash,
58+
_dailyAllowance: 40,
59+
_dayTotalWithdrawals: 0,
4160
deposit,
4261
withdraw,
4362
transferInto,
4463
reportBalance,
4564
getName,
65+
resetDailyAllowance,
66+
setDailyAllowance,
4667
};
4768
}
4869

4970
function main() {
50-
const walletJack = createWallet('Jack', 100);
51-
const walletJoe = createWallet('Joe', 10);
52-
const walletJane = createWallet('Jane', 20);
71+
const walletJack = createWallet("Jack", 100);
72+
const walletJoe = createWallet("Joe", 10);
73+
const walletJane = createWallet("Jane", 20);
5374

54-
walletJack.transferInto(walletJoe, 50);
55-
walletJane.transferInto(walletJoe, 25);
75+
walletJack.transferInto(walletJoe, 50); // OK
76+
walletJane.transferInto(walletJoe, 25); // Exceeds allowance
5677

5778
walletJane.deposit(20);
58-
walletJane.transferInto(walletJoe, 25);
79+
walletJane.transferInto(walletJoe, 25); // OK
5980

6081
walletJack.reportBalance();
6182
walletJoe.reportBalance();
6283
walletJane.reportBalance();
84+
85+
// Пример изменения лимита и сброса
86+
walletJane.setDailyAllowance(100);
87+
walletJane.resetDailyAllowance();
88+
walletJane.transferInto(walletJoe, 50); // Теперь OK
89+
90+
walletJane.reportBalance();
6391
}
6492

6593
main();

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

Lines changed: 7 additions & 5 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
function Wallet(name, cash) {
44
this._name = name;
@@ -26,7 +26,9 @@ Wallet.prototype.transferInto = function (wallet, amount) {
2626
} to ${wallet.getName()}`
2727
);
2828
const withdrawnAmount = this.withdraw(amount);
29-
wallet.deposit(withdrawnAmount);
29+
if (withdrawnAmount > 0) {
30+
wallet.deposit(withdrawnAmount);
31+
}
3032
};
3133

3234
Wallet.prototype.reportBalance = function () {
@@ -40,9 +42,9 @@ Wallet.prototype.getName = function () {
4042
};
4143

4244
function main() {
43-
const walletJack = new Wallet('Jack', 100);
44-
const walletJoe = new Wallet('Joe', 10);
45-
const walletJane = new Wallet('Jane', 20);
45+
const walletJack = new Wallet("Jack", 100);
46+
const walletJoe = new Wallet("Joe", 10);
47+
const walletJane = new Wallet("Jane", 20);
4648

4749
walletJack.transferInto(walletJoe, 50);
4850
walletJane.transferInto(walletJoe, 25);

Week4/prep-exercises/2-game-of-life/Cell.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ export default class Cell {
1414
this.y = y;
1515
this.alive = Math.random() > 0.5;
1616
this.nextAlive = false;
17+
this.lifeTime = this.alive ? 1 : 0;
1718
}
1819

1920
draw(context) {
20-
// Draw this background
21-
context.fillStyle = '#303030';
21+
context.fillStyle = "#303030";
2222
context.fillRect(
2323
this.x * Cell.size,
2424
this.y * Cell.size,
@@ -27,8 +27,12 @@ export default class Cell {
2727
);
2828

2929
if (this.alive) {
30-
// Draw living this inside background
31-
context.fillStyle = `rgb(24, 215, 236)`;
30+
let opacity = 0.25;
31+
if (this.lifeTime === 2) opacity = 0.5;
32+
else if (this.lifeTime === 3) opacity = 0.75;
33+
else if (this.lifeTime >= 4) opacity = 1;
34+
35+
context.fillStyle = `rgba(24, 215, 236, ${opacity})`;
3236
context.fillRect(
3337
this.x * Cell.size + 1,
3438
this.y * Cell.size + 1,
@@ -40,15 +44,20 @@ export default class Cell {
4044

4145
liveAndLetDie(aliveNeighbors) {
4246
if (aliveNeighbors === 2) {
43-
// Living cell remains living, dead cell remains dead
4447
this.nextAlive = this.alive;
4548
} else if (aliveNeighbors === 3) {
46-
// Dead cell becomes living, living cell remains living
4749
this.nextAlive = true;
4850
} else {
49-
// Living cell dies, dead cell remains dead
5051
this.nextAlive = false;
5152
}
53+
54+
if (this.alive && this.nextAlive) {
55+
this.lifeTime += 1;
56+
} else if (!this.alive && this.nextAlive) {
57+
this.lifeTime = 1;
58+
} else if (this.alive && !this.nextAlive) {
59+
this.lifeTime = 0;
60+
}
5261
}
5362

5463
update() {

0 commit comments

Comments
 (0)