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..606fb33 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -12,11 +12,31 @@ 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 -} + + 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 +48,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: 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 } } 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(); diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..edd1766 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -3,11 +3,15 @@ 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() { return this.#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() { 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)}` 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, 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);