diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js index f1d9169..982af8a 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -11,21 +11,12 @@ let rotations = 0; while (rotations < 2) { const currentState = trafficLight.state; console.log("The traffic light is on", currentState); - - // TODO - // if the color is green, turn it orange - // if the color is orange, turn it red - // if the color is red, add 1 to rotations and turn it green } -/** - * The output should be: +if (currentState === "green") { trafficLight.state = "orange"; -The traffic light is on green -The traffic light is on orange -The traffic light is on red -The traffic light is on green -The traffic light is on orange -The traffic light is on red +} else if (currentState === "orange") { trafficLight.state = "red"; -*/ + } else if (currentState === "red") { trafficLight.state = "green"; + rotations++; + } \ No newline at end of file diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js index 8c6ba95..5429fbf 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -1,9 +1,5 @@ "use strict"; -/** - * The `possibleStates` property define the states (in this case: colours) - * in which the traffic light can be. - * The `stateIndex` property indicates which of the possible states is current. - */ + const trafficLight = { possibleStates: ["green", "orange", "red"], stateIndex: 0, @@ -14,20 +10,14 @@ while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); - // TODO - // if the color is green, turn it orange - // if the color is orange, turn it red - // if the color is red, add 1 to cycles and turn it green -} - -/** - * The output should be: - -The traffic light is on green -The traffic light is on orange -The traffic light is on red -The traffic light is on green -The traffic light is on orange -The traffic light is on red + if (currentState === "green") + { trafficLight.stateIndex = 1; + } else if (currentState === "orange") + { trafficLight.stateIndex = 2 ; -*/ + } else if (currentState === "red") { + trafficLight.stateIndex = 0; + + cycle++; + } + } \ No newline at end of file diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..2fb5f0b 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -3,10 +3,14 @@ import eurosFormatter from './euroFormatter.js'; class Wallet { #name; #cash; + #dailyAllowance; + #dayTotalWithdrawals; constructor(name, cash) { this.#name = name; this.#cash = cash; + this.#dailyAllowance = 40; + this.#dayTotalWithdrawals = 0; } get name() { @@ -42,6 +46,19 @@ class Wallet { `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` ); } + resetDailyAllowance() { + this.#dayTotalWithdrawals = 0; + console.log( + `Daily allowance reset to: ${eurosFormatter.format(this.#dailyAllowance)}` + ); + } + setDailyAllowance(newAllowance) { + this.#dailyAllowance = newAllowance; + console.log( + `New daily withdrawal limit set to: ${eurosFormatter.format(this.#dailyAllowance)}` + ); + } + } function main() { @@ -49,14 +66,16 @@ function main() { const walletJoe = new Wallet('Joe', 10); const walletJane = new Wallet('Jane', 20); - walletJack.transferInto(walletJoe, 50); - walletJane.transferInto(walletJoe, 25); - - walletJane.deposit(20); - walletJane.transferInto(walletJoe, 25); + walletJack.setDailyAllowance(70); + walletJack.withdraw(10); + walletJack.reportBalance(); + walletJack.resetDailyAllowance(); + walletJack.withdraw(20); walletJack.reportBalance(); - walletJoe.reportBalance(); + + walletJane.deposit(50); + walletJane.transferInto(walletJoe, 40); walletJane.reportBalance(); } diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..d64fcb1 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -4,18 +4,25 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit: function (amount) { this._cash += amount; }, withdraw: function (amount) { + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } if (this._cash - amount < 0) { console.log(`Insufficient funds!`); return 0; } this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; }, @@ -38,23 +45,46 @@ function createWallet(name, cash = 0) { getName: function () { return this._name; }, - }; -} + resetDailyAllowance: function () { + this._dayTotalWithdrawals = 0; + console.log( + `Daily allowance reset to: ${eurosFormatter.format( + this._dailyAllowance + )}` + ); + }, + setDailyAllowance: function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `New daily withdrawal limit set to: ${eurosFormatter.format( + this._dailyAllowance + )}` + ); + }, + } + + + } + + function main() { 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.setDailyAllowance(70); + walletJack.withdraw(10); walletJack.reportBalance(); - walletJoe.reportBalance(); + walletJack.resetDailyAllowance(); + walletJack.withdraw(20); + walletJack.reportBalance(); + + walletJane.deposit(50); + walletJane.transferInto(walletJoe, 40); walletJane.reportBalance(); + walletJoe.reportBalance(); + } main(); diff --git a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js index bd4fd20..bad42cd 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -5,12 +5,18 @@ function deposit(amount) { } function withdraw(amount) { + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + if (this._cash - amount < 0) { console.log(`Insufficient funds!`); return 0; } this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; } @@ -34,15 +40,33 @@ function getName() { return this._name; } +function resetDailyAllowance() { + this._dayTotalWithdrawals = 0; + console.log( + `Daily allowance reset to: ${eurosFormatter.format(this._dailyAllowance)}` + ); +} + +function setDailyAllowance(newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `New daily withdrawal limit set to: ${eurosFormatter.format(this._dailyAllowance)}` + ); +} + function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit, withdraw, transferInto, reportBalance, getName, + resetDailyAllowance, + setDailyAllowance, }; } @@ -51,15 +75,17 @@ function main() { 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.setDailyAllowance(70); + walletJack.withdraw(10); + walletJack.reportBalance(); + walletJack.resetDailyAllowance(); + walletJack.withdraw(20); walletJack.reportBalance(); - walletJoe.reportBalance(); + + walletJane.deposit(50); + walletJane.transferInto(walletJoe, 40); walletJane.reportBalance(); + walletJoe.reportBalance(); } main(); diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..b572d2e 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -3,6 +3,8 @@ import eurosFormatter from './euroFormatter.js'; function Wallet(name, cash) { this._name = name; this._cash = cash; + this._dailyAllowance = 40; + this._dayTotalWithdrawals = 0; } Wallet.prototype.deposit = function (amount) { @@ -10,6 +12,10 @@ Wallet.prototype.deposit = function (amount) { }; Wallet.prototype.withdraw = function (amount) { + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } if (this._cash - amount < 0) { console.log(`Insufficient funds!`); return 0; @@ -39,20 +45,38 @@ Wallet.prototype.getName = function () { return this._name; }; +Wallet.prototype.resetDailyAllowance = function () { + this._dayTotalWithdrawals = 0; + console.log( + `Daily allowance reset to: ${eurosFormatter.format(this._dailyAllowance)}` + ); +} + +Wallet.prototype.setDailyAllowance = function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `New daily withdrawal limit set to: ${eurosFormatter.format(this._dailyAllowance)}` + ); +} + function main() { const walletJack = new Wallet('Jack', 100); const walletJoe = new Wallet('Joe', 10); const walletJane = new Wallet('Jane', 20); - walletJack.transferInto(walletJoe, 50); - walletJane.transferInto(walletJoe, 25); - - walletJane.deposit(20); - walletJane.transferInto(walletJoe, 25); + walletJack.setDailyAllowance(70); + walletJack.withdraw(10); + walletJack.reportBalance(); + walletJack.resetDailyAllowance(); + walletJack.withdraw(20); walletJack.reportBalance(); - walletJoe.reportBalance(); + + walletJane.deposit(50); + walletJane.transferInto(walletJoe, 40); walletJane.reportBalance(); + walletJoe.reportBalance(); + } main();