From 1e87db385fe054f016c79c90a0fee2759b2fe454 Mon Sep 17 00:00:00 2001 From: Nima Saghikalhori Date: Mon, 15 Jul 2024 21:40:59 +0200 Subject: [PATCH 1/2] prep-ex-week3 --- .../1-hyf-program/1-find-mentors.js | 17 ++++++---- .../1-hyf-program/2-class-list.js | 32 ++++++++++++++----- 2 files changed, 35 insertions(+), 14 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..4f4e133 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,13 @@ import { modules, students, mentors, classes } from "./hyf.js"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { - // TODO complete this function + const possibleMentors = mentors.filter(mentor => mentor.canTeach.includes(moduleName)); + + return possibleMentors.map(mentor => mentor.name); + }; -// 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 +23,9 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const possibleMentors = possibleMentorsForModule(moduleName); + const random = Math.floor(Math.random() * possibleMentors.length); + return possibleMentors[random]; }; -// 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..dab1ca7 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -1,5 +1,4 @@ import { modules, students, mentors, classes } from "./hyf.js"; - /** * We would like to have a list of everyone that is currently participating in a class. * This means the students, but also the mentors that are currently teaching the class. @@ -12,10 +11,21 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function -}; -// You can uncomment out this line to try your function -// console.log(getPeopleOfClass('class34')); + const classInfo = classes.find(classes => classes.name === className); + if (!classInfo) { + return[]; + } + const currentModule = classInfo.currentModule; + const classStudents = students .filter(student => student.class === className) + .map(student => ({ name: student.name, role:'student'})); + + const classMentors = mentors.filter( mentor => mentor.nowTeaching === currentModule) + .map(mentor => ({name: mentor.name, role: 'mentor'})); + + return [...classStudents, ...classMentors]; + }; + + console.log(getPeopleOfClass('class34')); /** * We would like to have a complete overview of the current active classes. @@ -30,7 +40,13 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - // TODO complete this function +const activeClasses = classes.filter(classes => classes.active); +const result = {}; + activeClasses.forEach(classes => { + result[classes.name] = getPeopleOfClass(classes.name); + }); + + return result; }; -// You can uncomment out this line to try your function -// console.log(getActiveClasses()); + + console.log(getActiveClasses()); \ No newline at end of file From 66c100160c6843e10cdba0b88314933cd6a5d917 Mon Sep 17 00:00:00 2001 From: Nima Saghikalhori Date: Mon, 22 Jul 2024 15:03:16 +0200 Subject: [PATCH 2/2] prep-ex-w4 --- Week4/prep-exercises/1-wallet/ex2-classes.js | 15 ++++++++++++++- Week4/prep-exercises/1-wallet/ex3-object.js | 11 +++++++++++ .../1-wallet/ex4-object-shared-methods.js | 18 ++++++++++++++++++ Week4/prep-exercises/1-wallet/ex5-prototype.js | 16 ++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..e8dfc79 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() { @@ -24,6 +28,7 @@ class Wallet { } this.#cash -= amount; + this.#dayTotalWithdrawals += amount; return amount; } @@ -37,9 +42,17 @@ class Wallet { wallet.deposit(withdrawnAmount); } + reportDailyAllowance () { + this.#dayTotalWithdrawals = 0; + } + + setDailyAllowance (newAllowance) { + this.#dailyAllowance = newAllowance; + } + reportBalance() { console.log( - `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` + `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}, daily allowance ${eurosFormatter.format(this.#dailyAllowance)}, total withdrawn today : ${eurosFormatter.format(this.#dayTotalWithdrawals)}` ); } } diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..711b274 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -4,6 +4,8 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance : 40, + dayTotalWithdrawals : 0, deposit: function (amount) { this._cash += amount; @@ -16,6 +18,7 @@ function createWallet(name, cash = 0) { } this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; }, @@ -38,6 +41,14 @@ function createWallet(name, cash = 0) { getName: function () { return this._name; }, + + resetDailyAllowance: function (newAllowance) { + this._dayTotalWithdrawals = 0; + }, + + setDailyAllowance : function (newAllowance) { + this._dailyAllowance = newAllowance; + }, }; } 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..dd889c9 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -10,7 +10,13 @@ function withdraw(amount) { return 0; } + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`surpassed daily allowance`); + return 0; + } + this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; } @@ -34,15 +40,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 }; } diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..faa833b 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) { @@ -15,7 +17,13 @@ Wallet.prototype.withdraw = function (amount) { return 0; } + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`surpassed daily allowance`); + return 0; + } + this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; }; @@ -39,6 +47,14 @@ Wallet.prototype.getName = function () { return this._name; }; +Wallet.prototype.setDailyAllowance = 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);