From bad0fc1e5da52289c3961bbb8b5f7c75e05f6874 Mon Sep 17 00:00:00 2001 From: Jasem Date: Fri, 3 Nov 2023 14:50:18 +0100 Subject: [PATCH 1/4] prep-exe-weeks1-js-finished --- Week1/prep-exercises/1-traffic-light/traffic-light-1.js | 9 ++++++++- Week1/prep-exercises/1-traffic-light/traffic-light-2.js | 9 ++++++++- 2 files changed, 16 insertions(+), 2 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..399c7be 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -11,7 +11,14 @@ let rotations = 0; while (rotations < 2) { const currentState = trafficLight.state; console.log("The traffic light is on", currentState); - + if (trafficLight.state === 'green'){ + trafficLight.state = 'orange'; + }else if (trafficLight.state === 'orange'){ + trafficLight.state = 'red'; + }else if (trafficLight.state === 'red'){ + rotations++; + trafficLight.state = 'green' + } // TODO // if the color is green, turn it orange // if the color is orange, turn it 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..4e43dc2 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -13,7 +13,14 @@ let cycle = 0; while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); - + if(currentState === 'green'){ + trafficLight.stateIndex = 1; + }else if (currentState === 'orange'){ + trafficLight.stateIndex = 2; + }else if (currentState === 'red'){ + cycle++; + trafficLight.stateIndex = 0; + } // TODO // if the color is green, turn it orange // if the color is orange, turn it red From 2b96db1f33e5931aa091405ce7eccebcfe801bd7 Mon Sep 17 00:00:00 2001 From: Jasem Date: Sat, 11 Nov 2023 14:24:55 +0100 Subject: [PATCH 2/4] week2 prep --- .../1-traffic-light/traffic-light.js | 14 +++++++++----- Week2/prep-exercises/2-experiments/index.js | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 7 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..8d23da8 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -7,16 +7,20 @@ function getCurrentState(trafficLight) { // TODO - // Should return the current state (i.e. colour) of the `trafficLight` + return(trafficLight.possibleStates[trafficLight.stateIndex]); // object passed as a parameter. } 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 nextIndex = 0; + const currentState = getCurrentState(trafficLight); + if(currentState === 'green'){ + return(nextIndex += 1); + }else if (currentState === 'orange'){ + return(nextIndex += 2); + }else return (nextIndex) } // This function loops for the number of seconds specified by the `secs` diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..0482137 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -6,7 +6,10 @@ function runExperiment(sampleSize) { // TODO // Write a for loop that iterates `sampleSize` times (sampleSize is a number). // In each loop iteration: - // + for(let i = 0; i < sampleSize; i++){ + const randomNum = Math.floor((Math.random()*6) + 1); + valueCounts[randomNum-1] += 1; + } // 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` @@ -16,6 +19,11 @@ function runExperiment(sampleSize) { const results = []; // TODO + for(let count of valueCounts){ + let countPer = ((count / sampleSize)*100).toFixed(2) + results.push(countPer); + } + // 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 @@ -25,13 +33,17 @@ function runExperiment(sampleSize) { // two decimals, e.g. '14.60'. // 3. Then push that string onto the `results` array. - return results; + return (results); } function main() { const sampleSizes = [100, 1000, 1000000]; // TODO + for(let sample of sampleSizes){ + console.log(runExperiment(sample),sample); + } + // 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 From 26e722f7205c815286fd9209af81bd65ed5c4219 Mon Sep 17 00:00:00 2001 From: Jasem Date: Sat, 18 Nov 2023 19:48:21 +0100 Subject: [PATCH 3/4] prep ex w3 --- .../1-hyf-program/1-find-mentors.js | 15 ++++---- .../1-hyf-program/2-class-list.js | 36 ++++++++++++++++--- 2 files changed, 40 insertions(+), 11 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..49c82de 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -1,4 +1,4 @@ -import { modules, students, mentors, classes } from "./hyf.js"; +import { modules, students, mentors, classes, mentors } from "./hyf.js"; /** * Tjebbe would like help to get a list of possible mentors for a module. @@ -8,10 +8,12 @@ import { modules, students, mentors, classes } from "./hyf.js"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { - // TODO complete this function -}; + + const mentorsList = mentors.filter(mentor => mentor.canTeach.includes(moduleName)).map(mentor => mentor.name); +return mentorsList +} // 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 +22,8 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const randomMentor = possibleMentorsForModule(moduleName); + return (randomMentor[Math.floor(Math.random() * randomMentor.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..07048c9 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,28 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function + const everyone = []; + const currentModule = classes.find((cl) => cl.name === className)?.currentModule; + + +students.forEach((student)=>{ + if(student.class == className && student.graduated == false){ + + everyone.push({name:student.name, rule: 'student'}) + } +}); + +mentors.forEach((mentor)=>{ + if(mentor.nowTeaching === currentModule){ + everyone.push({name:mentor.name, role:'mentor'}) + + } +}) +return everyone }; // 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. @@ -30,7 +48,15 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - // TODO complete this function -}; + + const activeClasses = classes.filter((cl) => cl.active === true); + + const activePeople = {}; + activeClasses.forEach((cl) => { + activePeople[cl.name] = getPeopleOfClass(cl.name); + }); + + return activePeople; +} // You can uncomment out this line to try your function -// console.log(getActiveClasses()); + console.log(getActiveClasses()); From e3ffc31c319619be6eb72e79b33a8f490a20fea7 Mon Sep 17 00:00:00 2001 From: Jasem Date: Sat, 25 Nov 2023 10:37:37 +0100 Subject: [PATCH 4/4] week4 --- .../1-wallet/ex1-closure-example.js | 2 +- Week4/prep-exercises/1-wallet/ex2-classes.js | 20 +++++++++++++++++++ Week4/prep-exercises/1-wallet/ex3-object.js | 16 +++++++++++++++ .../1-wallet/ex4-object-shared-methods.js | 16 +++++++++++++++ .../prep-exercises/1-wallet/ex5-prototype.js | 20 +++++++++++++++++++ 5 files changed, 73 insertions(+), 1 deletion(-) diff --git a/Week4/prep-exercises/1-wallet/ex1-closure-example.js b/Week4/prep-exercises/1-wallet/ex1-closure-example.js index e98b056..9baebcd 100644 --- a/Week4/prep-exercises/1-wallet/ex1-closure-example.js +++ b/Week4/prep-exercises/1-wallet/ex1-closure-example.js @@ -68,7 +68,7 @@ function createWallet(name, cash = 0) { } function main() { - const walletJack = createWallet("Jack", 100); + const walletJack = createWallet("Jack", 100); const walletJoe = createWallet("Joe", 10); const walletJane = createWallet("Jane", 20); diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..f0fb276 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -3,15 +3,21 @@ 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() { return this.#name; } + if deposit(amount) { this.#cash += amount; @@ -22,8 +28,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.#dayTotalWithdrawals += amount; return amount; } @@ -36,6 +47,15 @@ class Wallet { const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); } + + setDailyAllowance(newAllowance){ + this.#dailyAllowance = newAllowance; + console.log(`daily allowance ${eurosFormatter.format(newAllowance)}`) + } + resetDailyAllowance(){ + this.#dayTotalWithdrawals = 0; + } + reportBalance() { console.log( diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..75504e8 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -4,6 +4,9 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, + deposit: function (amount) { this._cash += amount; @@ -14,8 +17,13 @@ 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; }, @@ -29,6 +37,14 @@ function createWallet(name, cash = 0) { wallet.deposit(withdrawnAmount); }, + setDailyAllowance: function (newAllowance){ + this._dailyAllowance = newAllowance; + console.log(`Allowance set to ${eurosFormatter.format(newAllowance)} `) + }, + resetDailyAllowance: function(){ + this._dayTotalWithdrawals = 0; + }, + reportBalance: function () { console.log( `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` 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..299d967 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -9,8 +9,13 @@ 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; } @@ -29,6 +34,15 @@ function reportBalance() { `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` ); } + function setDailyAllowance(newAllowance){ + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance ${eurosFormatter.format(newAllowance)}` + ); + } + function resetDailyAllowance(){ + this._dayTotalWithdrawals = 0; + } function getName() { return this._name; @@ -38,6 +52,8 @@ 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..d829845 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._dayTotalWithdrawal = 0; } Wallet.prototype.deposit = function (amount) { @@ -14,8 +16,13 @@ Wallet.prototype.withdraw = function (amount) { console.log(`Insufficient funds!`); return 0; } + if(this._dayTotalWithdrawal + amount > this._dailyAllowance){ + console.log(`Insufficient remaining daily allowance!`); + return 0 ; + } this._cash -= amount; + this._dayTotalWithdrawal += amount; return amount; }; @@ -28,6 +35,17 @@ Wallet.prototype.transferInto = function (wallet, amount) { const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); }; +wallet.prototype.setDailyAllowance = function(newAllowance){ + this._dailyAllowance = newAllowance; + + console.log( + `Daily allowance ${eurosFormatter.format(newAllowance)}` + ); + +} +wallet.prototype.resetDailyAllowance = function(){ + this._dayTotalWithdrawal = 0 ; +}; Wallet.prototype.reportBalance = function () { console.log( @@ -35,6 +53,8 @@ Wallet.prototype.reportBalance = function () { ); }; + + Wallet.prototype.getName = function () { return this._name; };