From d614a6530db366fcf407cde90cd06223e922bd71 Mon Sep 17 00:00:00 2001 From: Mokaclub Date: Fri, 18 Aug 2023 20:12:13 +0200 Subject: [PATCH 1/5] prep-exr --- .../prep-exercises/1-traffic-light/traffic-light-1.js | 10 ++++++++++ .../prep-exercises/1-traffic-light/traffic-light-2.js | 8 ++++++++ 2 files changed, 18 insertions(+) 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..e903867 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -12,6 +12,16 @@ while (rotations < 2) { const currentState = trafficLight.state; console.log("The traffic light is on", currentState); + if (currentState === "green") { + trafficLight.state = "orange"; + } else if (currentState === "orange") { + trafficLight.state = "red" + ; + } else if (currentState === "red") { + rotations++; + trafficLight.state = "green" + ; + } // TODO // if the color is green, turn it orange // if the color is orange, turn it 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..ff3de28 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -14,6 +14,14 @@ while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); + if (currentState === "green") { + trafficLight.stateIndex = 1; + } else if (currentState === "orange") { + trafficLight.stateIndex = 2; + } else if (currentState === "red") { + cycle++; + trafficLight.stateIndex = 0; + } // TODO // if the color is green, turn it orange // if the color is orange, turn it red From 0a2526f064ef61987cfaca5670cfa9f1bd9f6f0d Mon Sep 17 00:00:00 2001 From: Mokaclub Date: Sat, 26 Aug 2023 19:45:02 +0200 Subject: [PATCH 2/5] week two prep exercies --- Week2/prep-exercises/2-experiments/index.js | 22 ++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..1ea8865 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -2,7 +2,6 @@ function runExperiment(sampleSize) { const valueCounts = [0, 0, 0, 0, 0, 0]; - // TODO // Write a for loop that iterates `sampleSize` times (sampleSize is a number). // In each loop iteration: @@ -13,6 +12,11 @@ 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] += 1; + } + const results = []; // TODO @@ -25,6 +29,11 @@ function runExperiment(sampleSize) { // two decimals, e.g. '14.60'. // 3. Then push that string onto the `results` array. + for (let count of valueCounts) { + const percentage = (count / sampleSize) * 100; + results.push(percentage.toFixed(2)); + } + return results; } @@ -36,11 +45,10 @@ function main() { // value of the `sampleSizes` array. // Log the results of each experiment as well as the experiment size to the // console. - // The expected output could look like this: - // - // [ '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 (let size of sampleSizes) { + const experimentResults = runExperiment(size); + console.log(experimentResults, size); + } } -main(); +main(); \ No newline at end of file From 01153249d399bd811cf23a07ff08c7c36867d03e Mon Sep 17 00:00:00 2001 From: Mokaclub Date: Sat, 26 Aug 2023 19:47:07 +0200 Subject: [PATCH 3/5] week two prep exercies --- Week2/prep-exercises/1-traffic-light/traffic-light.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..d619198 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -6,9 +6,11 @@ */ 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) { @@ -17,6 +19,7 @@ function getNextStateIndex(trafficLight) { // - 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 + return (trafficLight.stateIndex + 1) % trafficLight.possibleStates.length; } // This function loops for the number of seconds specified by the `secs` From ad3f2a767fe0bb3ba46b967c8972b43009340d7f Mon Sep 17 00:00:00 2001 From: Mokaclub Date: Sat, 2 Sep 2023 11:01:04 +0200 Subject: [PATCH 4/5] prep-ex-week3 --- .../1-hyf-program/1-find-mentors.js | 21 +++++++-- .../1-hyf-program/2-class-list.js | 44 +++++++++++++++++-- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js index 72baa61..ffa3e4e 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -8,11 +8,23 @@ import { modules, students, mentors, classes } from "./hyf.js"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { + + // Find mentors who can teach the specified module + + // const mentorsForModule = mentors.filter(mentor => mentor.canTeach.includes(moduleName)); + + // Extract and return the names of mentors + //const mentorNames = mentorsForModule.map(mentor => mentor.name); + + //return mentorNames; +//}; + + return mentors.filter(mentor=>mentor.canTeach.includes(moduleName)).map(mentor=>mentor.name) // TODO complete this function }; // You can uncomment out this line to try your function -// console.log(possibleMentorsForModule('using-apis')); - + console.log(possibleMentorsForModule('using-apis')); + console.log(possibleMentorsForModule('browsers')); /** * Tjebbe wants to make it even easier for himself. * Fill in this function that chooses a random mentor to teach the given module. @@ -20,7 +32,10 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { + const possibleMentors = possibleMentorsForModule(moduleName) + return possibleMentors[Math.floor(Math.random() * possibleMentors.length)]; // TODO complete this function }; // You can uncomment out this line to try your function -// console.log(findMentorForModule('javascript')); + console.log(findMentorForModule('javascript')); + console.log(findMentorForModule('react')); diff --git a/Week3/prep-exercises/1-hyf-program/2-class-list.js b/Week3/prep-exercises/1-hyf-program/2-class-list.js index 44d2798..994b387 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -13,9 +13,39 @@ import { modules, students, mentors, classes } from "./hyf.js"; */ const getPeopleOfClass = (className) => { // TODO complete this function + const selectedClass = classes.find((cls) => cls.name === className); + // const selectedClass = { + // name: "class34", + // startDate: "2-9-2021", + // active: true, + // currentModule: "react", + // } + if (!selectedClass || !selectedClass.active) { + return []; + } + const people = []; + // Add students + const classStudents = students.filter( + (student) => student.class === className + ); + // const classStudents = [{ name: "Rob", class: "class34", gitHubName: "robvk", graduated: false },] + classStudents.forEach((student) => + people.push({ name: student.name, role: "student" }) + ); + // people = [{ name: "Rob", role: "student" }] + // Add mentors + mentors.forEach((mentor) => { + if (mentor.nowTeaching === selectedClass.currentModule) { + people.push({ name: mentor.name, role: "mentor" }); + } + }); + // people = [{ name: "Rob", role: "student" }, { name: "Shriyans", role: "mentor" }] + return people; + + }; // You can uncomment out this line to try your function -// console.log(getPeopleOfClass('class34')); +console.log(getPeopleOfClass('class34')); /** * We would like to have a complete overview of the current active classes. @@ -30,7 +60,15 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - // TODO complete this function + const activeClasses = {}; + + classes.forEach((cls) => { + if (cls.active) { + activeClasses[cls.name] = getPeopleOfClass(cls.name); + } + }); + + return activeClasses; }; // You can uncomment out this line to try your function -// console.log(getActiveClasses()); + console.log(getActiveClasses()); From 2d716003bb16cc3c3c942157168dbbe36f7f9791 Mon Sep 17 00:00:00 2001 From: Mokaclub Date: Sat, 9 Sep 2023 10:24:38 +0200 Subject: [PATCH 5/5] henain-week4-exercises --- Week4/prep-exercises/1-wallet/ex2-classes.js | 36 +++++++++++++++++++ Week4/prep-exercises/1-wallet/ex3-object.js | 30 +++++++++++++++- .../1-wallet/ex4-object-shared-methods.js | 28 +++++++++++++++ .../prep-exercises/1-wallet/ex5-prototype.js | 22 +++++++++++- 4 files changed, 114 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..8e75bf3 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -1,12 +1,17 @@ import eurosFormatter from './euroFormatter.js'; + class Wallet { #name; #cash; + #dailyAllowance; + #dayTotalWithdrawals; constructor(name, cash) { this.#name = name; this.#cash = cash; + this.#dailyAllowance = 40; // Default daily allowance + this.#dayTotalWithdrawals = 0; } get name() { @@ -18,12 +23,18 @@ class Wallet { } withdraw(amount) { + if (this.#dayTotalWithdrawals + amount > this.#dailyAllowance) { + console.log(`Daily withdrawal limit exceeded!`); + return 0; + } + if (this.#cash - amount < 0) { console.log(`Insufficient funds!`); return 0; } this.#cash -= amount; + this.#dayTotalWithdrawals += amount; return amount; } @@ -42,6 +53,14 @@ class Wallet { `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` ); } + + resetDailyAllowance() { + this.#dayTotalWithdrawals = 0; + } + + setDailyAllowance(newAllowance) { + this.#dailyAllowance = newAllowance; + } } function main() { @@ -58,6 +77,23 @@ function main() { walletJack.reportBalance(); walletJoe.reportBalance(); walletJane.reportBalance(); + + // new day start reset daily allowance wallets. + walletJack.resetDailyAllowance(); + walletJoe.resetDailyAllowance(); + walletJane.resetDailyAllowance(); + + // update the daily allowance . + walletJane.setDailyAllowance(60); + console.log(`${walletJane.name}'s daily allowance updated to 60 EUR.`); + + // withdrawals again. + walletJack.transferInto(walletJoe, 50); + walletJane.transferInto(walletJoe, 40); + + walletJack.reportBalance(); + walletJoe.reportBalance(); + walletJane.reportBalance(); } main(); diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..0d17c51 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -4,18 +4,26 @@ 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(`Daily withdrawal limit reached for ${this._name}`); + return 0; + } + if (this._cash - amount < 0) { - console.log(`Insufficient funds!`); + console.log(`Insufficient funds for ${this._name}`); return 0; } this._cash -= amount; + this.dayTotalWithdrawals += amount; return amount; }, @@ -35,6 +43,14 @@ function createWallet(name, cash = 0) { ); }, + resetDailyAllowance: function () { + this.dayTotalWithdrawals = 0; + }, + + setDailyAllowance: function (newAllowance) { + this.dailyAllowance = newAllowance; + }, + getName: function () { return this._name; }, @@ -55,6 +71,18 @@ function main() { walletJack.reportBalance(); walletJoe.reportBalance(); walletJane.reportBalance(); + + // start new day + walletJack.resetDailyAllowance(); + walletJoe.resetDailyAllowance(); + walletJane.resetDailyAllowance(); + + // Change daily allowance for Jack + walletJack.setDailyAllowance(50); + + // withdraw exceeding allowance daily. + walletJack.withdraw(60); + walletJack.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..ac17dcf 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -1,5 +1,6 @@ import eurosFormatter from './euroFormatter.js'; + function deposit(amount) { this._cash += amount; } @@ -10,7 +11,13 @@ function withdraw(amount) { return 0; } + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Maximum daily withdrawal reached!`); + return 0; + } + this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; } @@ -34,15 +41,27 @@ function getName() { return this._name; } +function resetDailyAllowance() { + this._dayTotalWithdrawals = 0; +} + +function setDailyAllowance(newAllowance) { + this._dailyAllowance = newAllowance; +} + function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit, withdraw, transferInto, reportBalance, getName, + resetDailyAllowance, + setDailyAllowance, }; } @@ -57,9 +76,18 @@ function main() { walletJane.deposit(20); walletJane.transferInto(walletJoe, 25); + // Reset daily allowance for a new day + walletJack.resetDailyAllowance(); + walletJoe.resetDailyAllowance(); + walletJane.resetDailyAllowance(); + + // new daily allowance for Jack + walletJack.setDailyAllowance(50); + walletJack.reportBalance(); walletJoe.reportBalance(); walletJane.reportBalance(); } main(); + diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..95cac4f 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -1,8 +1,11 @@ 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) { @@ -15,7 +18,13 @@ Wallet.prototype.withdraw = function (amount) { return 0; } + if (this.dayTotalWithdrawals + amount > this.dailyAllowance) { + console.log(`Daily withdrawal limit exceeded!`); + return 0; + } + this._cash -= amount; + this.dayTotalWithdrawals += amount; return amount; }; @@ -26,7 +35,9 @@ Wallet.prototype.transferInto = function (wallet, amount) { } to ${wallet.getName()}` ); const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); + if (withdrawnAmount > 0) { + wallet.deposit(withdrawnAmount); + } }; Wallet.prototype.reportBalance = function () { @@ -39,6 +50,14 @@ 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); @@ -56,3 +75,4 @@ function main() { } main(); +