From 4ed4f8ca46d27a8e42d9900313bfb6060bd94e10 Mon Sep 17 00:00:00 2001 From: Burhan Elaldi Date: Fri, 10 Nov 2023 13:29:26 +0100 Subject: [PATCH 1/5] prep exercises from week2 is completed --- Week2/prep-exercises/2-experiments/index.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..75d8d83 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -7,6 +7,10 @@ function runExperiment(sampleSize) { // Write a for loop that iterates `sampleSize` times (sampleSize is a number). // In each loop iteration: // + for(let i=0; i Date: Fri, 10 Nov 2023 13:31:58 +0100 Subject: [PATCH 2/5] prep exercises from week2 is completed --- Week2/prep-exercises/1-traffic-light/traffic-light.js | 6 ++++++ 1 file changed, 6 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..6742a01 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -7,12 +7,18 @@ function getCurrentState(trafficLight) { // TODO + return trafficLight.possibleStates[trafficLight.stateIndex]; // Should return the current state (i.e. colour) of the `trafficLight` // object passed as a parameter. } function getNextStateIndex(trafficLight) { // TODO + if(trafficLight.stateIndex == 2){ + return 0; + }else{ + return (trafficLight.stateIndex = trafficLight.stateIndex + 1); + } // 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 From 2ebcee4b26ccbc25304cc44b5e33d73b18bbfecc Mon Sep 17 00:00:00 2001 From: burhan1997 Date: Sat, 18 Nov 2023 12:57:47 +0100 Subject: [PATCH 3/5] prep ex is completed --- .../1-hyf-program/1-find-mentors.js | 26 ++++++-- .../1-hyf-program/2-class-list.js | 62 +++++++++++++++++-- 2 files changed, 78 insertions(+), 10 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..614a5e4 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -8,10 +8,16 @@ import { modules, students, mentors, classes } from "./hyf.js"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { - // TODO complete this function + let mentorList = []; + + mentorList = mentors.filter((mentor) => mentor.canTeach.includes(moduleName)); + + const mentorsNames = mentorList.map((mentor) => mentor.name); + return mentorsNames; }; + // You can uncomment out this line to try your function -// console.log(possibleMentorsForModule('using-apis')); +console.log(possibleMentorsForModule('using-apis')); /** * Tjebbe wants to make it even easier for himself. @@ -20,7 +26,19 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + // Filter mentors who can teach the specified module + const availableMentors = mentors.filter((mentor) => mentor.canTeach.includes(moduleName)); + + // Check if there are mentors available for the module + if (availableMentors.length === 0) { + return 'No available mentors for this module'; + } + + // Choose a random mentor from the available ones + const randomMentor = availableMentors[Math.floor(Math.random() * availableMentors.length)]; + + // Return the name of the chosen mentor + return randomMentor.name; }; // You can uncomment out this line to try your function -// console.log(findMentorForModule('javascript')); +console.log(findMentorForModule('javascript')); 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..d05a6b5 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -12,10 +12,33 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function + + // Find the class with the specified name + const currentClass = classes.find((classInfo) => classInfo.name === className); + + // Check if the class exists + if (!currentClass) { + return 'Class not found'; + } + + // Get students in the class + const studentsInClass = students + .filter((student) => student.class === className) + .map((student) => ({ name: student.name, role: 'student' })); + + // Get mentors in the class + const mentorsInClass = mentors + .filter( + (mentor) => + mentor.nowTeaching === className || + (mentor.nowTeaching === currentClass.currentModule) + ) + .map((mentor) => ({ name: mentor.name, role: 'mentor' })); + + // Combine and return the array of names and roles + return [...studentsInClass, ...mentorsInClass]; }; -// 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 +53,34 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - // TODO complete this function + const activeClasses = classes.filter((cls) => cls.active); + + const result = {}; + + activeClasses.forEach((classInfo) => { + const { name: className, currentModule } = classInfo; + const peopleInClass = []; + + // Add students in the class + students.forEach((student) => { + if (student.class === className) { + peopleInClass.push({ name: student.name, role: 'student' }); + } + }); + + // Add mentors in the class + mentors.forEach((mentor) => { + if (mentor.canTeach && mentor.canTeach.includes(currentModule)) { + peopleInClass.push({ name: mentor.name, role: 'mentor' }); + } + }); + + // Add the array of people to the result object with the class name as the property + result[className] = peopleInClass; + }); + + return result; }; -// You can uncomment out this line to try your function -// console.log(getActiveClasses()); + + +console.log(getActiveClasses()); From 078dfc37eca3c7275b098d92e81f838ac86a5943 Mon Sep 17 00:00:00 2001 From: burhan1997 Date: Sat, 25 Nov 2023 23:11:25 +0100 Subject: [PATCH 4/5] prep ex is completed --- Week4/prep-exercises/1-wallet/ex2-classes.js | 25 +++++++++++++++---- Week4/prep-exercises/1-wallet/ex3-object.js | 17 ++++++++++++- .../1-wallet/ex4-object-shared-methods.js | 2 +- .../prep-exercises/1-wallet/ex5-prototype.js | 15 ++++++++++- Week4/prep-exercises/1-wallet/package.json | 12 +++++++++ 5 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 Week4/prep-exercises/1-wallet/package.json diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..9d86fb5 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -1,12 +1,14 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from "./euroFormatter.js"; class Wallet { #name; #cash; + dailyAllowance; constructor(name, cash) { this.#name = name; this.#cash = cash; + this.dailyAllowance = 40; } get name() { @@ -23,6 +25,11 @@ class Wallet { return 0; } + if (amount > this.dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + this.#cash -= amount; return amount; } @@ -37,6 +44,13 @@ class Wallet { wallet.deposit(withdrawnAmount); } + setDailyAllowance(newAllowance) { + this.dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + } + reportBalance() { console.log( `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` @@ -45,11 +59,12 @@ class Wallet { } function main() { - const walletJack = new Wallet('Jack', 100); - const walletJoe = new Wallet('Joe', 10); - const walletJane = new Wallet('Jane', 20); + const walletJack = new Wallet("Jack", 100); + const walletJoe = new Wallet("Joe", 10); + const walletJane = new Wallet("Jane", 20); walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80); walletJane.transferInto(walletJoe, 25); walletJane.deposit(20); @@ -60,4 +75,4 @@ function main() { walletJane.reportBalance(); } -main(); +main(); \ No newline at end of file diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..baaa8c9 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,9 +1,10 @@ -import eurosFormatter from './euroFormatter.js'; +iimport eurosFormatter from './euroFormatter.js'; function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, deposit: function (amount) { this._cash += amount; @@ -15,10 +16,22 @@ function createWallet(name, cash = 0) { return 0; } + if (amount > this._dailyAllowance) { + console.log(`Insufficient allowance!`); + return 0; + } + this._cash -= amount; return amount; }, + setDailyAllowance: function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + }, + transferInto: function (wallet, amount) { console.log( `Transferring ${eurosFormatter.format(amount)} from ${ @@ -46,6 +59,8 @@ function main() { const walletJoe = createWallet('Joe', 10); const walletJane = createWallet('Jane', 20); + walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80); walletJack.transferInto(walletJoe, 50); walletJane.transferInto(walletJoe, 25); 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..6174346 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -62,4 +62,4 @@ function main() { walletJane.reportBalance(); } -main(); +main(); \ No newline at end of file diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..e96bc4a 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -3,6 +3,7 @@ import eurosFormatter from './euroFormatter.js'; function Wallet(name, cash) { this._name = name; this._cash = cash; + this._dailyAllowance = 40; } Wallet.prototype.deposit = function (amount) { @@ -15,10 +16,20 @@ Wallet.prototype.withdraw = function (amount) { return 0; } + if (amount > this._dailyAllowance) { + console.log(`Insufficient allowance!`); + return 0; + } + this._cash -= amount; return amount; }; +Wallet.prototype.setDailyAllowance = function (newAllowance) { +this._dailyAllowance = newAllowance; +console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`); +}; + Wallet.prototype.transferInto = function (wallet, amount) { console.log( `Transferring ${eurosFormatter.format(amount)} from ${ @@ -44,6 +55,8 @@ function main() { const walletJoe = new Wallet('Joe', 10); const walletJane = new Wallet('Jane', 20); + walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80) walletJack.transferInto(walletJoe, 50); walletJane.transferInto(walletJoe, 25); @@ -55,4 +68,4 @@ function main() { walletJane.reportBalance(); } -main(); +main(); \ No newline at end of file diff --git a/Week4/prep-exercises/1-wallet/package.json b/Week4/prep-exercises/1-wallet/package.json new file mode 100644 index 0000000..b6155b0 --- /dev/null +++ b/Week4/prep-exercises/1-wallet/package.json @@ -0,0 +1,12 @@ +{ + "name": "1-wallet", + "version": "1.0.0", + "description": "> Created by the one and only Jim, you can find him on our [Slack](https://hackyourfuture.slack.com/team/U383PTTK9) and on [GitHub](https://github.com/remarcmij)!", + "main": "euroFormatter.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +} From a492eb98c3d960b4850de61f422aa3b17ce40f22 Mon Sep 17 00:00:00 2001 From: burhan1997 Date: Sat, 25 Nov 2023 23:16:47 +0100 Subject: [PATCH 5/5] prep ex is updated and completed --- Week4/prep-exercises/1-wallet/ex3-object.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index baaa8c9..99a3410 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,4 +1,4 @@ -iimport eurosFormatter from './euroFormatter.js'; +import eurosFormatter from './euroFormatter.js'; function createWallet(name, cash = 0) { return {