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..0d57210 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -12,6 +12,16 @@ while (rotations < 2) { const currentState = trafficLight.state; console.log("The traffic light is on", currentState); + if(currentState === 'green'){ + trafficLight.state = 'orange'; + console.log("The traffic light is on", currentState); + } else if(currentState === 'orange'){ + trafficLight.state = 'red'; + console.log("The traffic light is on", currentState); + } else if(currentState === '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..6dedbeb 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -14,6 +14,16 @@ while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); + if(currentState === 'green'){ + trafficLight.stateIndex++; + console.log("The traffic light is on", currentState); + } else if(currentState === 'orange'){ + trafficLight.stateIndex++; + console.log("The traffic light is on", currentState); + } else if(currentState === 'red'){ + cycle++; + trafficLight.stateIndex -= 2; + } // TODO // if the color is green, turn it orange // if the color is orange, turn it red diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..592343c 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -6,28 +6,23 @@ */ function getCurrentState(trafficLight) { - // TODO - // Should return the current state (i.e. colour) of the `trafficLight` - // object passed as a parameter. + trafficLight.possibleStates = [...trafficLight.possibleStates, ...trafficLight.possibleStates]; + 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 + if(getCurrentState(trafficLight) === 'green') { + return ++trafficLight.stateIndex; + } else if(getCurrentState(trafficLight) === 'orange') { + return ++trafficLight.stateIndex; + } else if(getCurrentState(trafficLight) === 'red') { + return ++trafficLight.stateIndex; + } } -// 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) { - // nothing do to here } } diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..6645acd 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -3,27 +3,17 @@ 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 { + for(let i= ind+1; i { + for(let i= ind+1; i{ + let count = 0; + for(let i=0; i= parseInt(passwordPolicy.times[0]) && count <= parseInt(passwordPolicy.times[2])){ + console.log(`${passwordPolicy.password} is VALID, ${passwordPolicy.letter} is present ${count} times and should have been present at least ${parseInt(passwordPolicy.times[0])} and at most ${parseInt(passwordPolicy.times[2])} times`); + } else{ + console.log(`${passwordPolicy.password} is INVALID, ${passwordPolicy.letter} is present ${count} times and should have been present at least ${parseInt(passwordPolicy.times[0])} and at most ${parseInt(passwordPolicy.times[2])} times`); + } +}) \ No newline at end of file diff --git a/Week3/challenges/4-bank-account.js b/Week3/challenges/4-bank-account.js index 8f0f035..3dc8b89 100644 --- a/Week3/challenges/4-bank-account.js +++ b/Week3/challenges/4-bank-account.js @@ -27,11 +27,28 @@ const bankAccount = { ], }; +const updateBankAccount = (amount, reason) => { + bankAccount.currentBalance = bankAccount.currentBalance - amount; + bankAccount.transactions.push({prevAmount: bankAccount.currentBalance + amount, + newAmount: bankAccount.currentBalance, + reason: reason}); +}; + const donateMoney = (amount, onSuccess, onFail) => { - // TODO complete this function + if(bankAccount.currentBalance>amount){ + onSuccess(); + updateBankAccount(amount, reason = 'Donation'); + } else{ + onFail(); + } }; const payRent = (amount, onSuccess, onFail) => { - // TODO complete this function + if(bankAccount.currentBalance>amount){ + onSuccess(); + updateBankAccount(amount, reason = 'Rent'); + } else{ + onFail(); + } }; /** 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..6a79a23 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -8,10 +8,13 @@ import { modules, students, mentors, classes } from "./hyf.js"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { - // TODO complete this function + const moduleTeachers = mentors + .filter(mentor => {return mentor.canTeach.includes(moduleName)}) + .map(mentor => {return mentor.name}); + return moduleTeachers; }; // 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 moduleMentor = possibleMentorsForModule(moduleName); + const random = moduleMentor[Math.floor(Math.random()*moduleMentor.length)]; + return random; }; // 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..144e9a3 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,26 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function + + const currentStudents = students.filter(student => {return student.class.includes(className)}); + const currentStudentsRole = currentStudents.map(student => { + student.role = 'student'; + return {name: student.name, role: student.role}}); + + const currentModule = classes + .filter(class_ => {return class_.name === className;}) + .map(class_ => {return class_.currentModule}); + + const currentMentor = mentors + .filter(mentor => {return mentor.nowTeaching === currentModule.toString()}) + .map(mentor => { + mentor.role = 'mentor'; + return {name: mentor.name, role: mentor.role}}); + + return [...currentStudentsRole,...currentMentor]; }; // 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 +46,16 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - // TODO complete this function + const activeClasses = classes + .filter(class_ => {return class_.active === true;}) + .map(class_ => {return class_.name}); + + const obj = {}; + + const people = activeClasses.map(activeClass => { + return obj[activeClass] = getPeopleOfClass(activeClass);}); + + return obj; }; // You can uncomment out this line to try your function -// console.log(getActiveClasses()); +console.log(getActiveClasses()); diff --git a/Week4/prep-exercises/1-wallet/euroFormatter.js b/Week4/prep-exercises/1-wallet/euroFormatter.mjs similarity index 100% rename from Week4/prep-exercises/1-wallet/euroFormatter.js rename to Week4/prep-exercises/1-wallet/euroFormatter.mjs diff --git a/Week4/prep-exercises/1-wallet/ex1-closure-example.js b/Week4/prep-exercises/1-wallet/ex1-closure-example.mjs similarity index 94% rename from Week4/prep-exercises/1-wallet/ex1-closure-example.js rename to Week4/prep-exercises/1-wallet/ex1-closure-example.mjs index e98b056..56a79eb 100644 --- a/Week4/prep-exercises/1-wallet/ex1-closure-example.js +++ b/Week4/prep-exercises/1-wallet/ex1-closure-example.mjs @@ -1,7 +1,8 @@ -import eurosFormatter from "./euroFormatter.js"; +import eurosFormatter from "./euroFormatter.mjs"; /** - * This is the closure way of doing things and we have already completed it for you so you don't need to do anything. + * This is the closure way of doing things and we have already completed it for you so + * you don't need to do anything. * We leave it here as an example of how your other implementations should work! */ diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.mjs similarity index 63% rename from Week4/prep-exercises/1-wallet/ex2-classes.js rename to Week4/prep-exercises/1-wallet/ex2-classes.mjs index f016137..3c50b3a 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.mjs @@ -1,12 +1,16 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from './euroFormatter.mjs'; class Wallet { #name; #cash; + #dailyAllowance; + #dayTotalWithdrawals; constructor(name, cash) { this.#name = name; this.#cash = cash; + this.#dailyAllowance = 40; + this.#dayTotalWithdrawals = 0; } get name() { @@ -22,8 +26,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; } @@ -37,6 +46,17 @@ class Wallet { wallet.deposit(withdrawnAmount); } + resetDailyAllowance(){ + this.#dayTotalWithdrawals = 0; + } + + 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)}` @@ -50,6 +70,9 @@ function main() { const walletJane = new Wallet('Jane', 20); walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80); + walletJack.transferInto(walletJoe, 50); + 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.mjs similarity index 65% rename from Week4/prep-exercises/1-wallet/ex3-object.js rename to Week4/prep-exercises/1-wallet/ex3-object.mjs index e94faac..9c26005 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.mjs @@ -1,9 +1,11 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from './euroFormatter.mjs'; function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit: function (amount) { this._cash += amount; @@ -14,8 +16,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 +36,17 @@ function createWallet(name, cash = 0) { wallet.deposit(withdrawnAmount); }, + resetDailyAllowance: function() { + this._dayTotalWithdrawals = 0; + }, + + setDailyAllowance: function (newAllowance){ + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + }, + reportBalance: function () { console.log( `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` @@ -47,6 +65,9 @@ function main() { const walletJane = createWallet('Jane', 20); walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80); + walletJack.transferInto(walletJoe, 50); + 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.mjs similarity index 64% rename from Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js rename to Week4/prep-exercises/1-wallet/ex4-object-shared-methods.mjs index bd4fd20..4e5a704 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.mjs @@ -1,4 +1,4 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from './euroFormatter.mjs'; function deposit(amount) { this._cash += amount; @@ -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; } @@ -24,6 +29,17 @@ function transferInto(wallet, amount) { wallet.deposit(withdrawnAmount); } +function resetDailyAllowance() { + this._dayTotalWithdrawals = 0; +} + +function setDailyAllowance(newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); +} + function reportBalance() { console.log( `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` @@ -38,9 +54,13 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit, withdraw, transferInto, + resetDailyAllowance, + setDailyAllowance, reportBalance, getName, }; @@ -52,6 +72,9 @@ function main() { const walletJane = createWallet('Jane', 20); walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80); + walletJack.transferInto(walletJoe, 50); + 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.mjs similarity index 65% rename from Week4/prep-exercises/1-wallet/ex5-prototype.js rename to Week4/prep-exercises/1-wallet/ex5-prototype.mjs index 7cba410..932786c 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.mjs @@ -1,8 +1,10 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from './euroFormatter.mjs'; function Wallet(name, cash) { this._name = name; this._cash = cash; + this._dailyAllowance = 40; + this._dayTotalWithdrawals = 0; } Wallet.prototype.deposit = function (amount) { @@ -14,8 +16,13 @@ Wallet.prototype.withdraw = function (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 +36,17 @@ Wallet.prototype.transferInto = function (wallet, 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: ${eurosFormatter.format(newAllowance)}` + ); +} + Wallet.prototype.reportBalance = function () { console.log( `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` @@ -45,6 +63,9 @@ function main() { const walletJane = new Wallet('Jane', 20); walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80); + walletJack.transferInto(walletJoe, 50); + walletJane.transferInto(walletJoe, 25); walletJane.deposit(20);