diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..09f2d7f 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -6,17 +6,12 @@ */ 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) { - // TODO - // 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 - // - if the color is red, it will turn to green +function getNextStateIndex(trafficLight) +{ + return (trafficLight.stateIndex + 1) % trafficLight.possibleStates.length; } // This function loops for the number of seconds specified by the `secs` 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..2351e72 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -7,9 +7,9 @@ import { modules, students, mentors, classes } from "./hyf.js"; * It should return an array of names. So something like: * ['John', 'Mary'] */ -const possibleMentorsForModule = (moduleName) => { - // TODO complete this function -}; +const possibleMentorsForModule = (moduleName) => mentors.filter(mentor => mentor.modules.includes(moduleName)).map(mentor => mentor.name); + + // You can uncomment out this line to try your function // console.log(possibleMentorsForModule('using-apis')); @@ -20,7 +20,10 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const possibleMentors = possibleMentorsForModule(moduleName); + if (possibleMentors.length === 0) return null; + const randomIndex = Math.floor(Math.random() * possibleMentors.length); + return possibleMentors[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 44d2798..13766c5 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -11,9 +11,23 @@ import { modules, students, mentors, classes } from "./hyf.js"; * * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ -const getPeopleOfClass = (className) => { - // TODO complete this function +const getPeopleOfClass = (className) => + { + const classObj = classes.find(c => c.name === className); + if (!classObj) { + console.error(`Class "${className}" not found.`); + return []; + }; + + const currentModule = classObj.currentModule; + const teachingMentors = mentors + .filter(mentor => mentor.nowTeaching === currentModule) + .map(mentor => ({ name: mentor.name, role: 'mentor' })); + const classStudents = classObj.students.map(student => ({ name: student, role: 'student' })); + + return [...classStudents, ...teachingMentors]; }; + // You can uncomment out this line to try your function // console.log(getPeopleOfClass('class34')); @@ -30,7 +44,14 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - // TODO complete this function + const activeClasses = classes.filter(c => c.active); + const result = {}; + activeClasses.forEach(classObj => { + result[classObj.name] = getPeopleOfClass(classObj.name); + }); + + return result; + }; // You can uncomment out this line to try your function // console.log(getActiveClasses()); diff --git a/Week4/prep-exercises/1-wallet/ex1-closure-example.js b/Week4/prep-exercises/1-wallet/ex1-closure-example.js index e98b056..dbda5a2 100644 --- a/Week4/prep-exercises/1-wallet/ex1-closure-example.js +++ b/Week4/prep-exercises/1-wallet/ex1-closure-example.js @@ -29,16 +29,11 @@ function createWallet(name, cash = 0) { return amount; } - function transferInto(wallet, amount) { - console.log( - `Transferring ${eurosFormatter.format( - amount - )} from ${name} to ${wallet.getName()}` - ); - const withdrawnAmount = withdraw(amount); - wallet.deposit(withdrawnAmount); + function resetDailyAllowance() { + dayTotalWithdrawals = 0; } + function setDailyAllowance(newAllowance) { dailyAllowance = newAllowance; console.log( @@ -46,15 +41,11 @@ function createWallet(name, cash = 0) { ); } - function resetDailyAllowance() { - dayTotalWithdrawals = 0; - } function reportBalance() { console.log(`Name: ${name}, balance: ${eurosFormatter.format(cash)}`); } - const getName = () => name; return { deposit, diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..64dbaca 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -1,42 +1,47 @@ import eurosFormatter from './euroFormatter.js'; class Wallet { - #name; - #cash; - constructor(name, cash) { this.#name = name; this.#cash = cash; - } - - get name() { - return this.#name; + this.dailyALLowance = 40; + this.dayTotalWithdrawals = 0; } deposit(amount) { - this.#cash += amount; + this._cash += amount; } withdraw(amount) { - if (this.#cash - amount < 0) { + if (this._cash - amount < 0) { console.log(`Insufficient funds!`); return 0; } - this.#cash -= amount; + if (this.dayTotalWithdrawals + amount > this.dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + + this._cash -= amount; + this.dayTotalWithdrawals += amount; return amount; } - transferInto(wallet, amount) { - console.log( - `Transferring ${eurosFormatter.format(amount)} from ${this.name} to ${ - wallet.name - }` - ); + transferInto(wallet, amount) { const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); } + resetDailyAllowance() { + this.dayTotalWithdrawals = 0; + } + + setDailyAllowance(newAllowance) { + this.dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${newAllowance}`); + } + reportBalance() { console.log( `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..6faefa7 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,11 +1,17 @@ import eurosFormatter from './euroFormatter.js'; function createWallet(name, cash = 0) { + let dailyAllowance = 40; + let dayTotalWithdrawals = 0; return { _name: name, _cash: cash, + dailyAllowance, + dayTotalWithdrawals, - deposit: function (amount) { + + + deposit(amount) { this._cash += amount; }, @@ -16,18 +22,26 @@ function createWallet(name, cash = 0) { } this._cash -= amount; + this.dayTotalWithdrawals += amount; return amount; }, transferInto: function (wallet, amount) { - console.log( - `Transferring ${eurosFormatter.format(amount)} from ${ - this._name - } to ${wallet.getName()}` - ); + const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); }, + resetDailyAllowance() { + this.dayTotalWithdrawals = 0; + }, + + setDailyAllowance(newAllowance) { + this.dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${newAllowance}`); + }, + + + reportBalance: function () { console.log( 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..02521ef 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -10,9 +10,18 @@ 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; -} +}; + + function transferInto(wallet, amount) { console.log( @@ -28,16 +37,32 @@ function reportBalance() { console.log( `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` ); -} +}; function getName() { return this._name; +}; + +function resetDailyAllowance() { + this._dayTotalWithdrawals = 0; + console.log(`${this._name}'s daily withdrawals have been reset.`); +} + +function setDailyAllowance(newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `${this._name}'s daily allowance set to: ${eurosFormatter.format( + newAllowance + )}` + ); } function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit, withdraw, transferInto, diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..921e6c3 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -1,8 +1,10 @@ import eurosFormatter from './euroFormatter.js'; -function Wallet(name, cash) { +function Wallet(name, cash = 0 ) { this._name = name; this._cash = cash; + this.dailyAllowance = 40; + this.dayTotalWithdrawals = 0; } Wallet.prototype.deposit = function (amount) { @@ -15,20 +17,30 @@ 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; }; Wallet.prototype.transferInto = function (wallet, amount) { - console.log( - `Transferring ${eurosFormatter.format(amount)} from ${ - this._name - } to ${wallet.getName()}` - ); const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); }; +Wallet.prototype.resetDailyAllowance = function() { + this.dayTotalWithdrawals = 0; +}; + +Wallet.prototype.setDailyAllowance = function(newAllowance) { + this.dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${newAllowance}`); +}; + Wallet.prototype.reportBalance = function () { console.log( `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}`