From a43ff10bcecafc4374964d4cd572e041f5f8bd8b Mon Sep 17 00:00:00 2001 From: ozlem Date: Sat, 4 May 2024 14:11:13 +0200 Subject: [PATCH 1/3] exercise is done --- .../1-hyf-program/1-find-mentors.js | 15 +++++-- .../1-hyf-program/2-class-list.js | 39 +++++++++++++++---- 2 files changed, 42 insertions(+), 12 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..b6e2d37 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,14 @@ import { modules, students, mentors, classes } from "./hyf.js"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { - // TODO complete this function + const mentorsForModule = mentors.filter((mentor) => mentor.teaches.includes(moduleName)); + const mentorNames = mentorsForModule.map((mentor) => mentor.name); + return mentorNames; }; + + // 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 +24,10 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const mentorsForModule=possibleMentorsForModule(moduleName) + const randomIndex= Math.floor(Math.random()*mentorsForModule.length) + + return mentorsForModule[randomIndex] }; // 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..0cd03dd 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. @@ -12,10 +12,25 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function + const classInfo = classes.find((c) => c.name === className); + + if (!classInfo) { + return []; + } + + const studentsInClass = classInfo.students.map((studentId) => ({ + name: students.find((student) => student.id === studentId).name, + role: 'student', + })); + + const mentorsInClass = mentors + .filter((mentor) => mentor.nowTeaching === classInfo.currentModule) + .map((mentor) => ({ name: mentor.name, role: 'mentor' })); + + 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. @@ -29,8 +44,16 @@ const getPeopleOfClass = (className) => { * class35: [{ name: 'Jane', role: 'student' }, { name: 'Steve', role: 'mentor' }] * } */ -const getActiveClasses = () => { - // TODO complete this function + const getActiveClasses = () => { + const activeClasses = classes.filter((c) => c.isActive); + + const activeClassesInfo = {}; + + activeClasses.forEach((classInfo) => { + activeClassesInfo[classInfo.name] = getPeopleOfClass(classInfo.name); + }); + + return activeClassesInfo; }; -// You can uncomment out this line to try your function -// console.log(getActiveClasses()); + +console.log(getActiveClasses()); \ No newline at end of file From 10df8efa7d63bb0583d0bfa1eb3f526b1e2bc47d Mon Sep 17 00:00:00 2001 From: ozlem Date: Sun, 5 May 2024 11:00:18 +0200 Subject: [PATCH 2/3] exercises done --- .../1-hyf-program/1-find-mentors.js | 22 ++++++----- .../1-hyf-program/2-class-list.js | 37 +++++++++---------- 2 files changed, 30 insertions(+), 29 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 b6e2d37..5ef6b23 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -7,15 +7,16 @@ import { modules, students, mentors, classes } from "./hyf.js"; * It should return an array of names. So something like: * ['John', 'Mary'] */ -const possibleMentorsForModule = (moduleName) => { - const mentorsForModule = mentors.filter((mentor) => mentor.teaches.includes(moduleName)); + + const possibleMentorsForModule = (moduleName) => { + const mentorsForModule = mentors.filter((mentor) => mentor.canTeach.includes(moduleName)); const mentorNames = mentorsForModule.map((mentor) => mentor.name); return mentorNames; }; + +console.log(possibleMentorsForModule('using-apis')); -// You can uncomment out this line to try your function -console.log(possibleMentorsForModule('using-apis')); /** * Tjebbe wants to make it even easier for himself. @@ -23,11 +24,12 @@ console.log(possibleMentorsForModule('using-apis')); * * It should return a single name. */ -const findMentorForModule = (moduleName) => { - const mentorsForModule=possibleMentorsForModule(moduleName) - const randomIndex= Math.floor(Math.random()*mentorsForModule.length) - - return mentorsForModule[randomIndex] + + + const findMentorForModule = (moduleName) => { + const mentorsForModule = possibleMentorsForModule(moduleName); + const randomIndex = Math.floor(Math.random() * mentorsForModule.length); + return mentorsForModule[randomIndex]; }; -// You can uncomment out this line to try your function + 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 0cd03dd..19efd47 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -11,26 +11,24 @@ import { modules, students, mentors, classes } from './hyf.js'; * * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ -const getPeopleOfClass = (className) => { - const classInfo = classes.find((c) => c.name === className); - if (!classInfo) { - return []; + + const getPeopleOfClass = (className) => { + const targetClass = classes.find((c) => c.name === className); + if (!targetClass) { + return []; } - const studentsInClass = classInfo.students.map((studentId) => ({ - name: students.find((student) => student.id === studentId).name, - role: 'student', - })); + const studentsInClass = students.filter((student) => student.class === className) + .map((student) => ({ name: student.name, role: 'student' })); - const mentorsInClass = mentors - .filter((mentor) => mentor.nowTeaching === classInfo.currentModule) - .map((mentor) => ({ name: mentor.name, role: 'mentor' })); + const mentorsInClass = mentors.filter((mentor) => mentor.nowTeaching === targetClass.currentModule) + .map((mentor) => ({ name: mentor.name, role: 'mentor' })); return [...studentsInClass, ...mentorsInClass]; -}; - - console.log(getPeopleOfClass('class34')); +}; + + console.log(getPeopleOfClass('class34')); /** * We would like to have a complete overview of the current active classes. @@ -44,16 +42,17 @@ const getPeopleOfClass = (className) => { * class35: [{ name: 'Jane', role: 'student' }, { name: 'Steve', role: 'mentor' }] * } */ - const getActiveClasses = () => { - const activeClasses = classes.filter((c) => c.isActive); + + const getActiveClasses = () => { + const activeClasses = classes.filter((c) => c.active); const activeClassesInfo = {}; - activeClasses.forEach((classInfo) => { - activeClassesInfo[classInfo.name] = getPeopleOfClass(classInfo.name); + activeClasses.forEach((c) => { + activeClassesInfo[c.name] = getPeopleOfClass(c.name); }); return activeClassesInfo; }; -console.log(getActiveClasses()); \ No newline at end of file + console.log(getActiveClasses()); \ No newline at end of file From a56eafed5760ce7a142a6c0447826b23d1271a51 Mon Sep 17 00:00:00 2001 From: ozlem Date: Fri, 10 May 2024 23:49:33 +0200 Subject: [PATCH 3/3] exercises done --- Week4/prep-exercises/1-wallet/ex2-classes.js | 26 ++++++++++++++-- Week4/prep-exercises/1-wallet/ex3-object.js | 23 ++++++++++++-- .../1-wallet/ex4-object-shared-methods.js | 31 +++++++++++++++++-- .../prep-exercises/1-wallet/ex5-prototype.js | 29 +++++++++++++---- 4 files changed, 95 insertions(+), 14 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..31e542f 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -1,12 +1,16 @@ -import eurosFormatter from './euroFormatter.js'; +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() { @@ -23,7 +27,13 @@ class Wallet { return 0; } + if (this.#dayTotalWithdrawals + amount > this.#dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + this.#cash -= amount; + this.#dayTotalWithdrawals += amount; return amount; } @@ -37,6 +47,15 @@ class Wallet { wallet.deposit(withdrawnAmount); } + setDailyAllowance(newAllowance) { + this.#dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`); + } + + resetDailyAllowance() { + this.#dayTotalWithdrawals = 0; + } + reportBalance() { console.log( `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` @@ -60,4 +79,5 @@ 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..7b3fd66 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,9 +1,12 @@ import eurosFormatter from './euroFormatter.js'; -function createWallet(name, cash = 0) { +function createWallet(name, cash = 0, dailyAllowance = 40) { + let dayTotalWithdrawals = 0; + return { _name: name, _cash: cash, + _dailyAllowance: dailyAllowance, deposit: function (amount) { this._cash += amount; @@ -15,7 +18,13 @@ function createWallet(name, cash = 0) { return 0; } + if (dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + this._cash -= amount; + dayTotalWithdrawals += amount; return amount; }, @@ -29,6 +38,15 @@ function createWallet(name, cash = 0) { wallet.deposit(withdrawnAmount); }, + setDailyAllowance: function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`); + }, + + resetDailyAllowance: function () { + dayTotalWithdrawals = 0; + }, + reportBalance: function () { console.log( `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` @@ -57,4 +75,5 @@ function main() { walletJane.reportBalance(); } -main(); + +main(); \ No newline at end of file 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..7f4570f 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -34,15 +34,40 @@ function getName() { return this._name; } -function createWallet(name, cash = 0) { +function createWallet(name, cash = 0, dailyAllowance = 40) { + let dayTotalWithdrawals = 0; + return { _name: name, _cash: cash, + _dailyAllowance: dailyAllowance, + deposit, - withdraw, + withdraw: function (amount) { + if (this._cash - amount < 0) { + console.log(`Insufficient funds!`); + return 0; + } + + if (dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + + this._cash -= amount; + dayTotalWithdrawals += amount; + return amount; + }, transferInto, reportBalance, getName, + setDailyAllowance: function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`); + }, + resetDailyAllowance: function () { + dayTotalWithdrawals = 0; + }, }; } @@ -62,4 +87,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..4e11dd1 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -1,25 +1,33 @@ import eurosFormatter from './euroFormatter.js'; -function Wallet(name, cash) { +function Wallet(name, cash, dailyAllowance = 40) { this._name = name; this._cash = cash; + this._dailyAllowance = dailyAllowance; + this._dayTotalWithdrawals = 0; } -Wallet.prototype.deposit = function (amount) { +Wallet.prototype.deposit = function(amount) { this._cash += amount; }; -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(`Insufficient remaining daily allowance!`); + 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 +37,25 @@ 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.setDailyAllowance = function(newAllowance) { + this._dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`); +}; + +Wallet.prototype.resetDailyAllowance = function() { + this._dayTotalWithdrawals = 0; +}; + function main() { const walletJack = new Wallet('Jack', 100); const walletJoe = new Wallet('Joe', 10);