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..07989c5 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -16,6 +16,14 @@ while (rotations < 2) { // 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 (trafficLight.state === "green") { + trafficLight.state = "orange"; + } else if (trafficLight.state === "orange") { + trafficLight.state = "red"; + } else { + trafficLight.state = "green"; + rotations += 1; + } } /** 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..dc37a24 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -18,6 +18,14 @@ while (cycle < 2) { // 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 (trafficLight.stateIndex == 0 ){ + trafficLight.stateIndex =1; + } else if (trafficLight.stateIndex == 1 ){ + trafficLight.stateIndex = 2; + } else { + trafficLight.stateIndex = 0; + cycle += 1; + } } /** diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..0c89983 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -6,12 +6,16 @@ */ function getCurrentState(trafficLight) { + + return trafficLight.possibleStates[trafficLight.stateIndex]; // TODO // Should return the current state (i.e. colour) of the `trafficLight` // object passed as a parameter. } function getNextStateIndex(trafficLight) { + + return (trafficLight.stateIndex + 1) % trafficLight.possibleStates.length; // TODO // Return the index of the next state of the `trafficLight` such that: // - if the color is green, it will turn to orange diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..d72cf45 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -12,6 +12,10 @@ function runExperiment(sampleSize) { // 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 { - // TODO complete this function + const mentorsForModule = mentors.filter(mentors => mentors.canTeach.includes(moduleName)); + return mentorsForModule.map(mentors=> mentors.name); }; -// You can uncomment out this line to try your function -// 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. * Fill in this function that chooses a random mentor to teach the given module. @@ -20,7 +24,14 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const mentorsForModule = possibleMentorsForModule(moduleName); + if (mentorsForModule.length === 0) { + console.log(`No mentors found for module '${moduleName}'.`); + return null; + } + + 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..b3bb167 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -1,36 +1,59 @@ -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. - * This means the students, but also the mentors that are currently teaching the class. - * The students should be self explanatory, but to find the mentors you will need to follow these steps: - * - Check what the `currentModule` of the class is - * - Find the mentor(s) that are `nowTeaching` that module - * - * Should return the list of names and their roles. So something like: - * - * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] - */ -const getPeopleOfClass = (className) => { - // TODO complete this function -}; -// You can uncomment out this line to try your function -// console.log(getPeopleOfClass('class34')); + /** + * We would like to have a list of everyone that is currently participating in a class. + * This means the students, but also the mentors that are currently teaching the class. + * The students should be self explanatory, but to find the mentors you will need to follow these steps: + * - Check what the `currentModule` of the class is + * - Find the mentor(s) that are `nowTeaching` that module + * + * Should return the list of names and their roles. So something like: + * + * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] + */ + const getPeopleOfClass = (className) => { + const classInfo = classes.find(classItem => classItem.name === className); + if (!classInfo) { + console.log(`Class '${className}' not found.`); + return []; + } -/** - * 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: - * - * { - * class34: [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }], - * class35: [{ name: 'Jane', role: 'student' }, { name: 'Steve', role: 'mentor' }] - * } - */ -const getActiveClasses = () => { - // TODO complete this function -}; -// You can uncomment out this line to try your function -// console.log(getActiveClasses()); + const studentsInClass = students.filter(student => student.class === className) + .map(student => ({ name: student.name, role: 'student' })); + + const mentorInClass = mentors.filter(mentor => mentor.nowTeaching === classInfo.currentModule) + .map(mentor => ({ name: mentor.name, role: 'mentor' })); + + return [...studentsInClass, ...mentorInClass]; + // TODO complete this function + }; + // You can uncomment out this line to try your function + 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: + * + * { + * class34: [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }], + * class35: [{ name: 'Jane', role: 'student' }, { name: 'Steve', role: 'mentor' }] + * } + */ + const getActiveClasses = () => { + const activeClasses = classes.filter(classItem => classItem.active); + + const activeClassesWithPeople = {}; + activeClasses.forEach(classItem => { + const peopleInClass = getPeopleOfClass(classItem.name); + activeClassesWithPeople[classItem.name] = peopleInClass; + }); + + return activeClassesWithPeople; + + + // TODO complete this function + }; + // You can uncomment out this line to try your function + console.log(getActiveClasses()); diff --git a/Week3/prep-exercises/1-hyf-program/trial2.js b/Week3/prep-exercises/1-hyf-program/trial2.js new file mode 100644 index 0000000..cb0aaec --- /dev/null +++ b/Week3/prep-exercises/1-hyf-program/trial2.js @@ -0,0 +1,80 @@ +import { modules, students, mentors, classes } from "./hyf.js"; + +/** + * We would like to have a list of everyone that is currently participating in a class. + * This means the students, but also the mentors that are currently teaching the class. + * The students should be self explanatory, but to find the mentors you will need to follow these steps: + * - Check what the `currentModule` of the class is + * - Find the mentor(s) that are `nowTeaching` that module + * + * Should return the list of names and their roles. So something like: + * + * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] + */ +const getPeopleOfClass = (className) => { + let subject, + student = [], + mentor = [], + classOf = []; + //let's get subject or currentModule of the selected class. + + let currentClass = classes.find( + (clas) => clas.name === className && clas.active + ); + + //if the currentClass variable is not null, or undefined, then it means className is actually active and we will call the function, activeClass() to get the students and mentors of that class. + if (!currentClass) { + console.log( + "this class is graduated, hence no active students or mentors." + ); + } else { + activeClass(); + } + //below there is a function that will gather students and mentors of the selelcted class and put them in an array. + + function activeClass() { + subject = currentClass.currentModule; + student = students.filter((stud) => stud.class === className); + + mentor = mentors.filter((ment) => ment.nowTeaching === subject); + + student.map((cursist) => { + const { name } = cursist; + classOf.push({ name, role: "student" }); + }); + mentor.map((teacher) => { + const { name } = teacher; + classOf.push({ name, role: "mentor" }); + }); + } + return classOf; +}; +// You can uncomment out this line to try your function +console.log(getPeopleOfClass("class32")); + +/** + * 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: + * + * { + * class34: [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }], + * class35: [{ name: 'Jane', role: 'student' }, { name: 'Steve', role: 'mentor' }] + * } + */ +const getActiveClasses = () => { + let currentActiveClasses = {}; + + classes.forEach((clas) => { + if (clas.active) { + currentActiveClasses[clas.name] = getPeopleOfClass(clas.name); + } + }); + + return currentActiveClasses; +}; + +// You can uncomment out this line to try your function +console.log(getActiveClasses()); \ No newline at end of file diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..56e54b6 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -1,17 +1,28 @@ -import eurosFormatter from './euroFormatter.js'; +//import eurosFormatter from './euroFormatter.js'; +const eurosFormatter = new Intl.NumberFormat('nl-NL', { + style: 'currency', + currency: 'EUR', +}); + 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 +33,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 +52,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( @@ -60,4 +85,4 @@ 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..4e9c2da 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,9 +1,17 @@ -import eurosFormatter from './euroFormatter.js'; +//import eurosFormatter from './euroFormatter.js'; +const eurosFormatter = new Intl.NumberFormat('nl-NL', { + style: 'currency', + currency: 'EUR', +}); + function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, + deposit: function (amount) { this._cash += amount; @@ -14,8 +22,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 +42,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)}` @@ -57,4 +78,4 @@ 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..33c8629 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,9 @@ -import eurosFormatter from './euroFormatter.js'; +//import eurosFormatter from './euroFormatter.js'; +const eurosFormatter = new Intl.NumberFormat('nl-NL', { + style: 'currency', + currency: 'EUR', +}); + function deposit(amount) { this._cash += amount; @@ -9,8 +14,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 +39,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 +57,8 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit, withdraw, transferInto, @@ -62,4 +83,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..1aa73c9 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -1,8 +1,15 @@ -import eurosFormatter from './euroFormatter.js'; +//import eurosFormatter from './euroFormatter.js'; +const eurosFormatter = new Intl.NumberFormat('nl-NL', { + style: 'currency', + currency: 'EUR', +}); + function Wallet(name, cash) { this._name = name; this._cash = cash; + this._dailyAllowance = 40; + this._dayTotalWithdrawal = 0; } Wallet.prototype.deposit = function (amount) { @@ -14,8 +21,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 +40,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 +58,8 @@ Wallet.prototype.reportBalance = function () { ); }; + + Wallet.prototype.getName = function () { return this._name; }; @@ -55,4 +80,4 @@ function main() { walletJane.reportBalance(); } -main(); +main(); \ No newline at end of file