diff --git a/Week1/prep-exercises/1-traffic-light/README.md b/Week1/prep-exercises/1-traffic-light/README.md index d994741..47d07fc 100644 --- a/Week1/prep-exercises/1-traffic-light/README.md +++ b/Week1/prep-exercises/1-traffic-light/README.md @@ -1,9 +1,6 @@ -# Prep exercise - traffic light +you can find the solutions in the follow links: -Let's have a deeper look at the working of traffic lights this week so that we can practice logic and loops. In `traffic-light-1.js` and `traffic-light-2.js` you will find the same requirements but with different ways of representing the traffic light. Have a look through the files and solve them so you can see how the way we represent data affects the way we need to solve problems. +1-[traffic-light-1.js](traffic-light-1.js) -## Things to think about -- Which way of representing the traffic light did you find better? Why? -- What happens if you change the loop to a `do-while` loop instead of a `while` loop? Why? -- We could have also used a `for` loop to make the traffic light do 2 full rotations. Do you think that would be better? Why or why not? +2-[traffic-light-2.js](traffic-light-2.js) 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..7fb94b7 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,32 @@ 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"){trafficLight.state = "green"}; +rotations++; + second method: + switch (currentState) { + case "greeen": + trafficlight.state = "orange"; + break; +} + switch (currentState) { + case "orange": + trafficlight.state = "red"; + break; + } + switch (currentState) { + case "red": + trafficlight.state = "green"; + rotations++; + break; + + default: + break; + + // 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..3150bfa 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -13,6 +13,13 @@ 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 (currentState === "red"){trafficLight.stateIndex = 0}; + cycle++; + + + } // TODO // if the color is green, turn it orange diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..bd031d0 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -9,14 +9,30 @@ 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) { + const currentStateIndex = trafficLight.stateIndex; // 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 + var nextStateIndex; + + switch (currentStateIndex) { + case 0: // Green + nextStateIndex = 1; + break; + case 1: // Orange + nextStateIndex = 2; + break; + case 2: // Red + nextStateIndex = 0; + break; + default: + console.error("Unexpected state index:", currentStateIndex); } // 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..e5e423e 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,11 @@ import { modules, students, mentors, classes } from "./hyf.js"; */ const possibleMentorsForModule = (moduleName) => { // TODO complete this function + const theMentors = mentors.filter((mentor) => mentors.ableToTeach.includes(moduleName) > 0); + return theMentors.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. @@ -21,6 +23,11 @@ const possibleMentorsForModule = (moduleName) => { */ const findMentorForModule = (moduleName) => { // TODO complete this function + const theMentors = mentors.filter((mentor) => mentors.ableToTeach.includes(moduleName) > 0); + if (theMentors.length > 0){ + const theWantedMentors = math.floor(math.random() * theMentors.length > 0); + return theMentors[theWantedMentors]; + } }; // 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..dac5b83 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -12,17 +12,49 @@ 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((class) => class.name === className); + + if (!classInfo) { + + return []; + } + + const studentsInClass = classInfo.students.map((studentN) => { + + + const student = students.find((stud) => stud.id === studentId); + + return { name: student.name, role: 'student' }; + }); + + const mentorsInClass = classInfo.mentors + .map((mentorId) => { + const mentor = mentors.find((m) => m.id === mentorId); + + const currentModule = modules.find( + (module) => module.name === classInfo.currentModule + ); + if (mentor.nowTeaching === currentModule.name) { + + return { name: mentor.name, role: 'mentor' }; + } + return null; + }) + .filter((mentor) => mentor !== null); + + 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. * First find the active classes, then for each get the people of that class. * * Should return an object with the class names as properties. - * Each class name property contains an array identical to the return from `getPeopleFromClass`. So something like: + * Each class name property contains an array identical to the return from `getPeopleFromClass`. + * So something like: * * { * class34: [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }], @@ -30,7 +62,18 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - // TODO complete this function + const activeClasses = classes.filter((class) => class.isActive); + + const result = {}; + activeClasses.forEach((class) => { + + const peopleInClass = getPeopleOfClass(class.name); + + result[class.name] = peopleInClass; + }); + + return result; }; + // You can uncomment out this line to try your function -// console.log(getActiveClasses()); +console.log(getActiveClasses()); diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..8cd78dc 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -1,12 +1,14 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from "./euroFormatter.js"; class Wallet { #name; #cash; + dailyAllowance; constructor(name, cash) { this.#name = name; this.#cash = cash; + this.dailyAllowance = 40; } get name() { @@ -23,6 +25,11 @@ class Wallet { return 0; } + if (amount > this.dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + this.#cash -= amount; return amount; } @@ -37,6 +44,13 @@ class Wallet { wallet.deposit(withdrawnAmount); } + setDailyAllowance(newAllowance) { + this.dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + } + reportBalance() { console.log( `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` @@ -45,11 +59,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..0d129fa 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,9 +1,10 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from "./euroFormatter.js"; function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, deposit: function (amount) { this._cash += amount; @@ -14,7 +15,10 @@ function createWallet(name, cash = 0) { console.log(`Insufficient funds!`); return 0; } - + if (amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } this._cash -= amount; return amount; }, @@ -28,6 +32,12 @@ function createWallet(name, cash = 0) { const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); }, + setDailyAllowance: function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + }, reportBalance: function () { console.log( @@ -42,9 +52,11 @@ 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.transferInto(walletJoe, 50); walletJane.transferInto(walletJoe, 25); diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..35faeca 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -1,8 +1,9 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from "./euroFormatter.js"; function Wallet(name, cash) { this._name = name; this._cash = cash; + this._dailyAllowance = 40; } Wallet.prototype.deposit = function (amount) { @@ -14,7 +15,10 @@ Wallet.prototype.withdraw = function (amount) { console.log(`Insufficient funds!`); return 0; } - + if (amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } this._cash -= amount; return amount; }; @@ -25,6 +29,12 @@ Wallet.prototype.transferInto = function (wallet, amount) { this._name } to ${wallet.getName()}` ); + Wallet.prototype.setDailyAllowance = function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + }; const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); }; @@ -40,10 +50,12 @@ 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); + walletJack.setDailyAllowance(80); walletJack.transferInto(walletJoe, 50); walletJane.transferInto(walletJoe, 25);