From d8a163b2e0004e68e9f028e547613d5a3d194f8e Mon Sep 17 00:00:00 2001 From: lidyaT21 <156967948+lidyaT21@users.noreply.github.com> Date: Thu, 21 Nov 2024 02:08:21 +0100 Subject: [PATCH 1/4] modified prep --- .../1-traffic-light/traffic-light-1.js | 24 ++++++------------ .../1-traffic-light/traffic-light-2.js | 25 ++++++------------- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js index f1d9169..de8c16e 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -12,20 +12,12 @@ while (rotations < 2) { const currentState = trafficLight.state; console.log("The traffic light is on", currentState); - // TODO - // if the color is green, turn it orange - // if the color is orange, turn it red - // if the color is red, add 1 to rotations and turn it green + if (currentState === "green") { + trafficLight.state = "orange"; + } else if (currentState === "orange") { + trafficLight.state = "red"; + } else { + trafficLight.state = "green"; + rotations++; + } } - -/** - * The output should be: - -The traffic light is on green -The traffic light is on orange -The traffic light is on red -The traffic light is on green -The traffic light is on orange -The traffic light is on red - -*/ diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js index 8c6ba95..ee24b62 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -13,21 +13,12 @@ let cycle = 0; while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); - - // TODO - // if the color is green, turn it orange - // if the color is orange, turn it red - // if the color is red, add 1 to cycles and turn it green + if (currentState === "green") { + trafficLight.stateIndex++; + } else if (currentState === "orange") { + trafficLight.stateIndex++; + } else { + trafficLight.stateIndex -= 2; + cycle++; + } } - -/** - * The output should be: - -The traffic light is on green -The traffic light is on orange -The traffic light is on red -The traffic light is on green -The traffic light is on orange -The traffic light is on red - -*/ From 64b391c31466f1e9a7942410e285f0e548bdd0ad Mon Sep 17 00:00:00 2001 From: lidyaT21 <156967948+lidyaT21@users.noreply.github.com> Date: Fri, 29 Nov 2024 20:55:47 +0100 Subject: [PATCH 2/4] modified prep --- .../1-traffic-light/traffic-light.js | 28 ++++--------- Week2/prep-exercises/2-experiments/index.js | 41 ++++++------------- 2 files changed, 21 insertions(+), 48 deletions(-) diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..c7c535b 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -1,29 +1,19 @@ "use strict"; -/** - * The `trafficLight` object is now no longer a global variable. Instead, - * it is defined in function `main()` and passed as a parameter to other - * functions, as and when needed. - */ - 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 + let state = trafficLight.stateIndex; + if (getCurrentState(trafficLight) === "green") { + return ++state; + } else if (getCurrentState(trafficLight) === "orange") { + return ++state; + } else { + return (state -= 2); + } } -// This function loops for the number of seconds specified by the `secs` -// parameter and then returns. -// IMPORTANT: This is not the recommended way to implement 'waiting' in -// JavaScript. You will learn better ways of doing this when you learn about -// asynchronous code. function waitSync(secs) { const start = Date.now(); while (Date.now() - start < secs * 1000) { diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..3a0d4b0 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -3,44 +3,27 @@ function runExperiment(sampleSize) { const valueCounts = [0, 0, 0, 0, 0, 0]; - // TODO - // Write a for loop that iterates `sampleSize` times (sampleSize is a number). - // In each loop iteration: - // - // 1. Generate a random integer between 1 and 6 (as if throwing a six-sided die). - // 2. Add `1` to the element of the `valueCount` that corresponds to the random - // value from the previous step. Use the first element of `valueCounts` - // for keeping a count how many times the value 1 is thrown, the second - // element for value 2, etc. + for (let i = 0; i < sampleSize; i++) { + let randomNumber = Math.floor(Math.random() * 6 + 1); + valueCounts[randomNumber - 1] += 1; + } const results = []; - // TODO - // Write a for..of loop for the `valueCounts` array created in the previous - // loop. In each loop iteration: - // 1. For each possible value of the die (1-6), compute the percentage of how - // many times that value was thrown. Remember that the first value of - // `valueCounts` represent the die value of 1, etc. - // 2. Convert the computed percentage to a number string with a precision of - // two decimals, e.g. '14.60'. - // 3. Then push that string onto the `results` array. + for (let count of valueCounts) { + let percentage = (count / sampleSize) * 100; + results.push(percentage.toFixed(2)); + } return results; } function main() { const sampleSizes = [100, 1000, 1000000]; - - // TODO - // Write a for..of loop that calls the `runExperiment()` function for each - // value of the `sampleSizes` array. - // Log the results of each experiment as well as the experiment size to the - // console. - // The expected output could look like this: - // - // [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100 - // [ '14.60', '17.10', '19.30', '15.50', '16.70', '16.80' ] 1000 - // [ '16.71', '16.68', '16.69', '16.66', '16.67', '16.59' ] 1000000 + for (let sampleSize of sampleSizes) { + let result = runExperiment(sampleSize); + console.log(result, sampleSize); + } } main(); From 0335b38110b5d2812640f2b8c8f28295b527f68e Mon Sep 17 00:00:00 2001 From: lidyaT21 <156967948+lidyaT21@users.noreply.github.com> Date: Fri, 6 Dec 2024 04:35:55 +0100 Subject: [PATCH 3/4] finished prep --- .../1-hyf-program/1-find-mentors.js | 11 +++++-- .../1-hyf-program/2-class-list.js | 29 +++++++++++++++++-- 2 files changed, 34 insertions(+), 6 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..5c95e24 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -9,9 +9,12 @@ import { modules, students, mentors, classes } from "./hyf.js"; */ const possibleMentorsForModule = (moduleName) => { // TODO complete this function + return mentors + .filter((mentor) => mentor.canTeach.includes(moduleName)) + .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 mentorCanTeach = possibleMentorsForModule(moduleName); + + return mentorCanTeach[Math.floor(Math.random() * mentorCanTeach.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..817223f 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,27 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function + const newClass = classes.find((cohrt) => cohrt.name.includes(className)); + const newStudent = students + .filter((student) => student.class.includes(className)) + .map((student) => { + return { + name: student.name, + role: "student", + }; + }); + const newMentor = mentors + .filter((mentor) => mentor.nowTeaching === newClass.currentModule) + .map((mentor) => { + return { + name: mentor.name, + role: "mentor", + }; + }); + return [...newStudent, ...newMentor]; }; // 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. @@ -31,6 +48,12 @@ const getPeopleOfClass = (className) => { */ const getActiveClasses = () => { // TODO complete this function + const activeClasses = classes.filter((Class) => Class.active === true); + + return activeClasses.reduce((result, Class) => { + result[Class.name] = getPeopleOfClass(Class.name); + return result; + }, {}); }; // You can uncomment out this line to try your function -// console.log(getActiveClasses()); +console.log(getActiveClasses()); From 2db0b81a15636abebe96664d0a998edd8b1afaaf Mon Sep 17 00:00:00 2001 From: lidyaT21 <156967948+lidyaT21@users.noreply.github.com> Date: Fri, 13 Dec 2024 04:43:25 +0100 Subject: [PATCH 4/4] finishedPrepEx --- Week4/prep-exercises/1-wallet/ex2-classes.js | 31 +++++++++++++++---- Week4/prep-exercises/1-wallet/ex3-object.js | 21 ++++++++++--- .../1-wallet/ex4-object-shared-methods.js | 26 +++++++++++++--- .../prep-exercises/1-wallet/ex5-prototype.js | 18 ++++++++--- 4 files changed, 77 insertions(+), 19 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..e5baa97 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() { @@ -22,8 +26,12 @@ 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.#dayTotalWithdrawals += amount; return amount; } @@ -37,6 +45,16 @@ 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)}` @@ -45,11 +63,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); diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..da62b85 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,9 +1,11 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from "./euroFormatter.js"; function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit: function (amount) { this._cash += amount; @@ -29,6 +31,16 @@ 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 () { + this._dayTotalWithdrawals = 0; + }, reportBalance: function () { console.log( `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` @@ -42,11 +54,12 @@ function createWallet(name, cash = 0) { } function main() { - const walletJack = createWallet('Jack', 100); - const walletJoe = createWallet('Joe', 10); - const walletJane = createWallet('Jane', 20); + const walletJack = createWallet("Jack", 100); + const walletJoe = createWallet("Joe", 10); + const walletJane = createWallet("Jane", 20); walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80); walletJane.transferInto(walletJoe, 25); walletJane.deposit(20); 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..4f73352 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -1,4 +1,4 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from "./euroFormatter.js"; function deposit(amount) { this._cash += amount; @@ -9,8 +9,12 @@ function withdraw(amount) { 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; } @@ -33,25 +37,37 @@ function reportBalance() { function getName() { return this._name; } +function resetDailyAllowance() { + this._dayTotalWithdrawals = 0; +} + +function setDailyAllowance(amount) { + this._dailyAllowance = amount; +} function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit, withdraw, transferInto, + setDailyAllowance, + resetDailyAllowance, reportBalance, getName, }; } function main() { - const walletJack = createWallet('Jack', 100); - const walletJoe = createWallet('Joe', 10); - const walletJane = createWallet('Jane', 20); + const walletJack = createWallet("Jack", 100); + const walletJoe = createWallet("Joe", 10); + const walletJane = createWallet("Jane", 20); walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80); walletJane.transferInto(walletJoe, 25); walletJane.deposit(20); diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..8f507b6 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'; +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) { @@ -29,6 +31,14 @@ Wallet.prototype.transferInto = function (wallet, amount) { wallet.deposit(withdrawnAmount); }; +Wallet.prototype.setDailyAllowance = function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`); +}; + +Wallet.prototype.resetDailyAllowance = function () { + this._dayTotalWithdrawals = 0; +}; Wallet.prototype.reportBalance = function () { console.log( `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` @@ -40,9 +50,9 @@ Wallet.prototype.getName = function () { }; 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); walletJane.transferInto(walletJoe, 25);