From 6bc8ee9ad6a0b07022951c17620c382596514305 Mon Sep 17 00:00:00 2001 From: hhana1-a Date: Sat, 27 Jan 2024 22:21:17 +0100 Subject: [PATCH 1/9] challenge solution --- .../1-traffic-light/traffic-light-1.js | 31 +++++++++++++++---- .../1-traffic-light/traffic-light-2.js | 23 +++++++++++++- 2 files changed, 47 insertions(+), 7 deletions(-) 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..8461e8f 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -4,19 +4,38 @@ * that moment. */ const trafficLight = { - state: "green", -}; + state: "green"}; let rotations = 0; -while (rotations < 2) { - const currentState = trafficLight.state; +while (rotations <2) { + let 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 -} + + switch (currentState) { + + case "green": + trafficLight.state = "orange"; + break; + + case "orange": + trafficLight.state = "red"; + break; + + case "red": + trafficLight.state = "green"; + rotations ++; + + + }} + + /** * The output should be: @@ -28,4 +47,4 @@ The traffic light is on green The traffic light is on orange The traffic light is on red -*/ +*/ 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..c182ce7 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -14,11 +14,32 @@ while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); + switch(currentState) { + + case "green": + trafficLight.stateIndex++; + break; + + case "orange": + trafficLight.stateIndex++; + break; + + case "red": + trafficLight.stateIndex=0; + cycle++; + + } + + +} + + + // 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: From 3edbc33a873a6d0fa4c107ef5f511d22f6af1231 Mon Sep 17 00:00:00 2001 From: hhana1-a Date: Sat, 27 Jan 2024 22:26:09 +0100 Subject: [PATCH 2/9] change --- Week1/prep-exercises/1-traffic-light/traffic-light-1.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 8461e8f..606fb33 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -4,11 +4,12 @@ * that moment. */ const trafficLight = { - state: "green"}; + state: "green", +}; let rotations = 0; -while (rotations <2) { - let currentState = trafficLight.state; +while (rotations < 2) { + const currentState = trafficLight.state; console.log("The traffic light is on", currentState); From 29561c0585da3e7c4346fe570c5e44abc37c4484 Mon Sep 17 00:00:00 2001 From: Hana <150429887+hhana1-a@users.noreply.github.com> Date: Sat, 3 Feb 2024 22:55:35 +0100 Subject: [PATCH 3/9] Update traffic-light.js --- .../1-traffic-light/traffic-light.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..390c422 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -6,17 +6,13 @@ */ function getCurrentState(trafficLight) { - // TODO - // Should return the current state (i.e. colour) of the `trafficLight` - // object passed as a parameter. + return trafficLight.possibleStates[trafficLight.stateIndex]; } function getNextStateIndex(trafficLight) { - // TODO - // Return the index of the next state of the `trafficLight` such that: - // - if the color is green, it will turn to orange - // - if the color is orange, it will turn to red - // - if the color is red, it will turn to green + trafficLight.stateIndex++; + trafficLight.stateIndex > 2 && (trafficLight.stateIndex = 0); + return trafficLight.stateIndex; } // This function loops for the number of seconds specified by the `secs` @@ -27,7 +23,7 @@ function getNextStateIndex(trafficLight) { function waitSync(secs) { const start = Date.now(); while (Date.now() - start < secs * 1000) { - // nothing do to here + // Nothing to do here, just waiting } } From 2be5c393e4a612414cc3ab08e3f4aae06ccc7f9b Mon Sep 17 00:00:00 2001 From: Hana <150429887+hhana1-a@users.noreply.github.com> Date: Sat, 3 Feb 2024 23:48:58 +0100 Subject: [PATCH 4/9] change --- Week2/prep-exercises/2-experiments/index.js | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..652604b 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -13,6 +13,14 @@ function runExperiment(sampleSize) { // for keeping a count how many times the value 1 is thrown, the second // element for value 2, etc. + for (let i = 0; i < sampleSize; i++) { + const randomValue = Math.floor(Math.random() * 6) + 1; + valueCounts[randomValue - 1]++; + } + + + + const results = []; // TODO @@ -25,12 +33,21 @@ function runExperiment(sampleSize) { // two decimals, e.g. '14.60'. // 3. Then push that string onto the `results` array. + + for (const count of valueCounts) { + const percentage = (count / sampleSize * 100).toFixed(2); + results.push(percentage); + } + + + return results; } function main() { const sampleSizes = [100, 1000, 1000000]; + // TODO // Write a for..of loop that calls the `runExperiment()` function for each // value of the `sampleSizes` array. @@ -41,6 +58,13 @@ function main() { // [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100 // [ '14.60', '17.10', '19.30', '15.50', '16.70', '16.80' ] 1000 // [ '16.71', '16.68', '16.69', '16.66', '16.67', '16.59' ] 1000000 + + for (const size of sampleSizes) { + const experimentResults = runExperiment(size); + console.log(experimentResults, size); + } + + } main(); From 60c47ff88a2ba44497db61a481ad19b3d1c95c96 Mon Sep 17 00:00:00 2001 From: Hana <150429887+hhana1-a@users.noreply.github.com> Date: Sat, 17 Feb 2024 19:40:35 +0100 Subject: [PATCH 5/9] Update ex2-classes.js --- Week4/prep-exercises/1-wallet/ex2-classes.js | 35 ++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..59a3ea2 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) { + constructor(name, cash, dailyAllowance = 40) { this.#name = name; this.#cash = cash; + this.#dailyAllowance = dailyAllowance; + this.#dayTotalWithdrawals = 0; } get name() { @@ -15,6 +19,7 @@ class Wallet { deposit(amount) { this.#cash += amount; + this.checkResetDailyAllowance(); } withdraw(amount) { @@ -23,7 +28,13 @@ class Wallet { return 0; } + if (this.#dayTotalWithdrawals + amount > this.#dailyAllowance) { + console.log(`Exceeds daily withdrawal limit!`); + return 0; + } + this.#cash -= amount; + this.#dayTotalWithdrawals += amount; return amount; } @@ -34,7 +45,9 @@ class Wallet { }` ); const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); + if (withdrawnAmount > 0) { + wallet.deposit(withdrawnAmount); + } } reportBalance() { @@ -42,6 +55,24 @@ class Wallet { `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` ); } + + resetDailyAllowance() { + this.#dayTotalWithdrawals = 0; + } + + setDailyAllowance(newAllowance) { + this.#dailyAllowance = newAllowance; + } + + checkResetDailyAllowance() { + if (this.#dayTotalWithdrawals === 0) return; + const now = new Date(); + const lastWithdrawalDate = new Date(); + lastWithdrawalDate.setTime(now.getTime() - 24 * 60 * 60 * 1000); // 24 hours ago + if (lastWithdrawalDate.getDate() !== now.getDate()) { + this.resetDailyAllowance(); + } + } } function main() { From d34880b4e0eb772ab3405ae36c115a140c590e27 Mon Sep 17 00:00:00 2001 From: Hana <150429887+hhana1-a@users.noreply.github.com> Date: Sat, 17 Feb 2024 19:41:15 +0100 Subject: [PATCH 6/9] update --- Week4/prep-exercises/1-wallet/ex2-classes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index 59a3ea2..edd1766 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -11,7 +11,7 @@ class Wallet { this.#cash = cash; this.#dailyAllowance = dailyAllowance; this.#dayTotalWithdrawals = 0; - } + } get name() { return this.#name; From 485be2f4e03d1d1fae1eceaf683da6a37634aa53 Mon Sep 17 00:00:00 2001 From: Hana <150429887+hhana1-a@users.noreply.github.com> Date: Sat, 17 Feb 2024 20:05:17 +0100 Subject: [PATCH 7/9] ex3 --- Week4/prep-exercises/1-wallet/ex3-object.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..75504e8 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -4,6 +4,9 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, + deposit: function (amount) { this._cash += amount; @@ -14,8 +17,13 @@ function createWallet(name, cash = 0) { console.log(`Insufficient funds!`); return 0; } + if(this._dayTotalWithdrawals + amount > this._dailyAllowance){ + console.log(`Insufficient remaining daily allowance!`); + return 0; + } this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; }, @@ -29,6 +37,14 @@ function createWallet(name, cash = 0) { wallet.deposit(withdrawnAmount); }, + setDailyAllowance: function (newAllowance){ + this._dailyAllowance = newAllowance; + console.log(`Allowance set to ${eurosFormatter.format(newAllowance)} `) + }, + resetDailyAllowance: function(){ + this._dayTotalWithdrawals = 0; + }, + reportBalance: function () { console.log( `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` From 9a7b8a886346951a889da6ecad185f7ed4ea0968 Mon Sep 17 00:00:00 2001 From: Hana <150429887+hhana1-a@users.noreply.github.com> Date: Sat, 17 Feb 2024 20:18:47 +0100 Subject: [PATCH 8/9] change --- .../1-wallet/ex4-object-shared-methods.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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..299d967 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -9,8 +9,13 @@ function withdraw(amount) { console.log(`Insufficient funds!`); return 0; } + if(this._dayTotalWithdrawals + amount > this._dailyAllowance){ + console.log(`Insufficient remaining daily allowance!`); + return 0; + } this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; } @@ -29,6 +34,15 @@ function reportBalance() { `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` ); } + function setDailyAllowance(newAllowance){ + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance ${eurosFormatter.format(newAllowance)}` + ); + } + function resetDailyAllowance(){ + this._dayTotalWithdrawals = 0; + } function getName() { return this._name; @@ -38,6 +52,8 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit, withdraw, transferInto, From d942c047bed8592532343e3363dd14ed771c952a Mon Sep 17 00:00:00 2001 From: Hana <150429887+hhana1-a@users.noreply.github.com> Date: Sat, 17 Feb 2024 20:27:22 +0100 Subject: [PATCH 9/9] update --- .../prep-exercises/1-wallet/ex5-prototype.js | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..c9eb9ec 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -3,23 +3,32 @@ 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) { +Wallet.prototype.deposit = function(amount) { this._cash += amount; + this.checkResetDailyAllowance(); }; -Wallet.prototype.withdraw = function (amount) { +Wallet.prototype.withdraw = function(amount) { if (this._cash - amount < 0) { console.log(`Insufficient funds!`); return 0; } + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Exceeds daily withdrawal limit!`); + return 0; + } + this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; }; -Wallet.prototype.transferInto = function (wallet, amount) { +Wallet.prototype.transferInto = function(wallet, amount) { console.log( `Transferring ${eurosFormatter.format(amount)} from ${ this._name @@ -29,16 +38,26 @@ Wallet.prototype.transferInto = function (wallet, amount) { wallet.deposit(withdrawnAmount); }; -Wallet.prototype.reportBalance = function () { +Wallet.prototype.reportBalance = function() { console.log( `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` ); }; -Wallet.prototype.getName = function () { +Wallet.prototype.getName = function() { return this._name; }; +Wallet.prototype.resetDailyAllowance = function() { + this._dayTotalWithdrawals = 0; +}; + +Wallet.prototype.setDailyAllowance = function(newAllowance) { + this._dailyAllowance = newAllowance; +}; + + + function main() { const walletJack = new Wallet('Jack', 100); const walletJoe = new Wallet('Joe', 10);