From faa697b318ce2459d69f06af9801a449c13f23cd Mon Sep 17 00:00:00 2001 From: Ilias Khugaev Date: Tue, 25 Feb 2025 17:42:36 +0100 Subject: [PATCH 1/2] solve prep 1 and 2 --- .../1-hyf-program/1-find-mentors.js | 16 +++++-- .../1-hyf-program/2-class-list.js | 47 +++++++++++++++++-- 2 files changed, 53 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..d06ed0e 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,12 @@ import { modules, students, mentors, classes } from "./hyf.js"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { - // TODO complete this function + const mentorsCanTeach = mentors + .filter(methods => methods.canTeach.some(method => method === moduleName)) + .map(mentor => mentor.name); + return mentorsCanTeach; }; -// You can uncomment out this line to try your function -// console.log(possibleMentorsForModule('using-apis')); +console.log(possibleMentorsForModule('javascript')); /** * Tjebbe wants to make it even easier for himself. @@ -20,7 +22,11 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const mentorsCanTeach = mentors + .filter(methods => methods.canTeach.some(method => method === moduleName)) + .map(mentor => mentor.name); + return mentorsCanTeach[(Math.floor(Math.random() * mentorsCanTeach.length))]; }; // 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..6d7cb64 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -1,4 +1,4 @@ -import { modules, students, mentors, classes } from "./hyf.js"; +import { modules, students, mentors, classes} from "./hyf.js"; /** * We would like to have a list of everyone that is currently participating in a class. @@ -10,9 +10,38 @@ import { modules, students, mentors, classes } from "./hyf.js"; * Should return the list of names and their roles. So something like: * * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] + * + * */ + +// const mentorsCanTeach = mentors +// .filter(methods => methods.canTeach.some(method => method === moduleName)) +// .map(mentor => mentor.name); +// return mentorsCanTeach; + const getPeopleOfClass = (className) => { - // TODO complete this function + + const studentsInClass = students.filter(student => student.class === className) + .map(student => student.name); + + const moduleName = classes.filter(whatClassIs => whatClassIs.name === className) + .map(module => module.currentModule)[0]; + + const mentorName = mentors.filter(mod => mod.nowTeaching === moduleName) + .map(TeacherName => TeacherName.name)[0]; + + const listOfNames = [ + { + name: studentsInClass[0], + role: 'student' + }, + { + name: mentorName, + role: 'mentor' + + }, + ]; + return listOfNames; }; // You can uncomment out this line to try your function // console.log(getPeopleOfClass('class34')); @@ -29,8 +58,16 @@ const getPeopleOfClass = (className) => { * class35: [{ name: 'Jane', role: 'student' }, { name: 'Steve', role: 'mentor' }] * } */ + const getActiveClasses = () => { - // TODO complete this function + const activeClasses = classes.filter(cls => cls.active); + + const result = {}; + + activeClasses.forEach(element => { + result[element.name] = getPeopleOfClass(element.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 1946046dc7785a2c0a1a84f3739f2c58d1329967 Mon Sep 17 00:00:00 2001 From: Ilias Khugaev Date: Mon, 3 Mar 2025 11:55:39 +0100 Subject: [PATCH 2/2] solve prep for week 4 --- Week4/prep-exercises/1-wallet/ex2-classes.js | 26 ++++++++++++-- Week4/prep-exercises/1-wallet/ex3-object.js | 36 ++++++++++++++++--- .../1-wallet/ex4-object-shared-methods.js | 22 ++++++++++++ .../prep-exercises/1-wallet/ex5-prototype.js | 20 +++++++++++ 4 files changed, 96 insertions(+), 8 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..12080dd 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() { @@ -22,8 +26,13 @@ class Wallet { console.log(`Insufficient funds!`); return 0; } - + if (this.#dayTotalWithdrawals + amount > this.#dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + this.#cash -= amount; + this.#dailyAllowance += amount; return amount; } @@ -37,11 +46,22 @@ class Wallet { wallet.deposit(withdrawnAmount); } - reportBalance() { + setDailyAllowance(newAllowance) { + this.#dailyAllowance = newAllowance; console.log( - `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` ); } + + resetDailyAllowance() { + this.#dailyAllowance = 0; + } + + reportBalance() { + console.log( + `Name: ${this.#name}, balance: ${eurosFormatter.format(this.#cash)}`); + } + } function main() { diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..85c3d51 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -4,6 +4,12 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, + + getName: function () { + return this._name; + }, deposit: function (amount) { this._cash += amount; @@ -14,8 +20,14 @@ 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; }, @@ -26,18 +38,29 @@ function createWallet(name, cash = 0) { } to ${wallet.getName()}` ); const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); + if (withdrawnAmount > 0) { + wallet.deposit(withdrawnAmount); + } + }, + + setDailyAllowance: function (newAllowance = 40) { + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + }, + + resetDailyAllowance: function () { + this._dayTotalWithdrawals = 0; }, + reportBalance: function () { console.log( `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` ); }, - getName: function () { - return this._name; - }, }; } @@ -55,6 +78,9 @@ function main() { walletJack.reportBalance(); walletJoe.reportBalance(); walletJane.reportBalance(); + walletJack.resetDailyAllowance(); + walletJoe.setDailyAllowance(50); + walletJane.setDailyAllowance(); } 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..3e95419 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(`Insufficient remaining daily allowance!`); + return 0; + } + this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; } @@ -34,15 +40,28 @@ function getName() { return this._name; } +function resetDailyAllowance() { + this._dayTotalWithdrawals = 0; +} + +function setDailyAllowance(newAllowance = 40) { + this._dailyAllowance = newAllowance; + console.log(`Daily allowance updated to ${newAllowance}.`); +} + function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit, withdraw, transferInto, reportBalance, getName, + resetDailyAllowance, + setDailyAllowance, }; } @@ -60,6 +79,9 @@ function main() { walletJack.reportBalance(); walletJoe.reportBalance(); walletJane.reportBalance(); + walletJack.resetDailyAllowance(); + walletJoe.setDailyAllowance(60); + walletJane.setDailyAllowance(); } main(); diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..293d428 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(`Insufficient remaining daily allowance!`); + return 0; + } + this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; }; @@ -28,6 +36,9 @@ Wallet.prototype.transferInto = function (wallet, amount) { const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); }; +Wallet.prototype.resetDailyAllowance = function () { + this._dayTotalWithdrawals = 0; +}; Wallet.prototype.reportBalance = function () { console.log( @@ -35,6 +46,12 @@ Wallet.prototype.reportBalance = function () { ); }; +Wallet.prototype.setDailyAllowance = function (newAllowance = 40) { + this._dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`); +}; + + Wallet.prototype.getName = function () { return this._name; }; @@ -53,6 +70,9 @@ function main() { walletJack.reportBalance(); walletJoe.reportBalance(); walletJane.reportBalance(); + + walletJack.resetDailyAllowance(); + walletJack.withdraw(60); } main();