diff --git a/Week3/challenges/1-sum-entries.js b/Week3/challenges/1-sum-entries.js index f7dd419..d739493 100644 --- a/Week3/challenges/1-sum-entries.js +++ b/Week3/challenges/1-sum-entries.js @@ -1,17 +1,28 @@ -/** - * Credit to https://adventofcode.com/ for this exercise - -In the list below you have an array of numbers. The goal is to find the two numbers that add up to 2020. - -Once you have found those numbers, multiply the numbers and store the result of that in the result variable. - */ - - -const list = [1721, 979, 366, 299, 675, 1456]; -let result; - -// Write your code here - - -// TEST CODE, do not change +/** + * Credit to https://adventofcode.com/ for this exercise + +In the list below you have an array of numbers. The goal is to find the two numbers that add up to 2020. + +Once you have found those numbers, multiply the numbers and store the result of that in the result variable. + */ + + +const list = [1721, 979, 366, 299, 675, 1456]; +let result; + +// Write your code here + +const someFunction = (year) => { + year.forEach((element) => { + for (let i = 1; i < year.length; i++) { + if (element + year[i] === 2020) { + result = element * year[i] + } + } + }); +}; + +someFunction(list); + +// TEST CODE, do not change console.assert(result === 514579, `The result is not correct, it is ${result}, but should be 514579`); \ No newline at end of file diff --git a/Week3/challenges/2-sum-three-entries.js b/Week3/challenges/2-sum-three-entries.js index f5f8773..edf8ec2 100644 --- a/Week3/challenges/2-sum-three-entries.js +++ b/Week3/challenges/2-sum-three-entries.js @@ -1,17 +1,32 @@ -/** - * Credit to https://adventofcode.com/ for this exercise - -In the list below you have an array of numbers. The goal is to find the three numbers that add up to 2020. - -Once you have found those numbers, multiply the numbers and store the result of that in the result variable. - */ - - -const list = [1721, 979, 366, 299, 675, 1456]; -let result; - -// Write your code here - - -// TEST CODE, do not change +/** + * Credit to https://adventofcode.com/ for this exercise + +In the list below you have an array of numbers. The goal is to find the three numbers that add up to 2020. + +Once you have found those numbers, multiply the numbers and store the result of that in the result variable. + */ + + +const list = [1721, 979, 366, 299, 675, 1456]; +let result; + +// Write your code here +const someFunction = (year) => { + year.forEach((element) => { + for (let i = 0; i < year.length; i++) { + if (element + year[i] < 2020) { + for (let j = 0; j < year.length; j++) { + if (element + year[i] + year[j] === 2020) { + result = element * year[i] * year[j]; + return result + } + } + } + } + }); +}; + +someFunction(list); + +// TEST CODE, do not change console.assert(result === 241861950, `The result is not correct, it is ${result}, but should be 241861950`); \ No newline at end of file diff --git a/Week3/challenges/3-password-validation.js b/Week3/challenges/3-password-validation.js index fa7ad11..ed78048 100644 --- a/Week3/challenges/3-password-validation.js +++ b/Week3/challenges/3-password-validation.js @@ -20,4 +20,18 @@ const passwordList = [ { times: '1-3', letter: 'a', password: 'abcde'}, { times: '1-3', letter: 'b', password: 'cdefg'}, { times: '2-9', letter: 'c', password: 'ccccccccc'} -]; \ No newline at end of file +]; + + const passwordPolicy= (policy) => { + policy.forEach(element => { + const [min, max] = element.times.split("-").map(Number); + let letters = element.password.split(element.letter).length - 1; + + if (letters >= min && letters <= max) { + console.log(`${element.password} is VALID, ${element.letter} is present ${letters} times and should have been present at least ${[min]} and at most ${[max]} times`); + } else { + console.log(`${element.password} is INVALID, "${element.letter}" is present ${letters} times and should have been present at least ${[min]} and at most ${[max]} times`); + } + }); + } +console.log(passwordPolicy(passwordList)); diff --git a/Week3/challenges/4-bank-account.js b/Week3/challenges/4-bank-account.js index 8f0f035..15bcb0b 100644 --- a/Week3/challenges/4-bank-account.js +++ b/Week3/challenges/4-bank-account.js @@ -27,12 +27,19 @@ const bankAccount = { ], }; -const donateMoney = (amount, onSuccess, onFail) => { - // TODO complete this function +const transaction = () => { + bankAccount.currentBalance -= amount; + bankAccount.transactions.push( {prevAmount: bankAccount.transactions[0].prevAmount - amount, + newAmount: bankAccount.transactions[0].newAmount - amount, + reason: reason, + }) }; + +const donateMoney = (amount, onSuccess, onFail) => { + +}; const payRent = (amount, onSuccess, onFail) => { - // TODO complete this function -}; + }; /** * TEST CODE. DO NOT EDIT @@ -61,33 +68,33 @@ console.log(bankAccount); donateMoney(100, onSuccessDutch, onFailDutch); console.log(bankAccount); -/** -* The console should print out the following: -Payment successful! Thank you! -{ -currentBalance: 150, -transactions: [ - { prevAmount: 350, newAmount: 250, reason: 'Donation' }, - { prevAmount: 250, newAmount: 150, reason: 'Donation' } -] -} -Payment successful! Thank you! -{ -currentBalance: 50, -transactions: [ - { prevAmount: 350, newAmount: 250, reason: 'Donation' }, - { prevAmount: 250, newAmount: 150, reason: 'Donation' }, - { prevAmount: 150, newAmount: 50, reason: 'Rent' } -] -} -U heeft niet voldoende saldo om deze betaling te doen. -{ -currentBalance: 50, -transactions: [ - { prevAmount: 350, newAmount: 250, reason: 'Donation' }, - { prevAmount: 250, newAmount: 150, reason: 'Donation' }, - { prevAmount: 150, newAmount: 50, reason: 'Rent' } -] -} -* -*/ +// /** +// * The console should print out the following: +// Payment successful! Thank you! +// { +// currentBalance: 150, +// transactions: [ +// { prevAmount: 350, newAmount: 250, reason: 'Donation' }, +// { prevAmount: 250, newAmount: 150, reason: 'Donation' } +// ] +// } +// Payment successful! Thank you! +// { +// currentBalance: 50, +// transactions: [ +// { prevAmount: 350, newAmount: 250, reason: 'Donation' }, +// { prevAmount: 250, newAmount: 150, reason: 'Donation' }, +// { prevAmount: 150, newAmount: 50, reason: 'Rent' } +// ] +// } +// U heeft niet voldoende saldo om deze betaling te doen. +// { +// currentBalance: 50, +// transactions: [ +// { prevAmount: 350, newAmount: 250, reason: 'Donation' }, +// { prevAmount: 250, newAmount: 150, reason: 'Donation' }, +// { prevAmount: 150, newAmount: 50, reason: 'Rent' } +// ] +// } +// * +// */ 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..d06ed0e 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,12 @@ import { modules, students, mentors, classes } from "./hyf.js"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { - // TODO complete this function + const mentorsCanTeach = mentors + .filter(methods => methods.canTeach.some(method => method === moduleName)) + .map(mentor => mentor.name); + return mentorsCanTeach; }; -// You can uncomment out this line to try your function -// console.log(possibleMentorsForModule('using-apis')); +console.log(possibleMentorsForModule('javascript')); /** * Tjebbe wants to make it even easier for himself. @@ -20,7 +22,11 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const mentorsCanTeach = mentors + .filter(methods => methods.canTeach.some(method => method === moduleName)) + .map(mentor => mentor.name); + return mentorsCanTeach[(Math.floor(Math.random() * mentorsCanTeach.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..6d7cb64 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -1,4 +1,4 @@ -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. @@ -10,9 +10,38 @@ import { modules, students, mentors, classes } from "./hyf.js"; * Should return the list of names and their roles. So something like: * * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] + * + * */ + +// const mentorsCanTeach = mentors +// .filter(methods => methods.canTeach.some(method => method === moduleName)) +// .map(mentor => mentor.name); +// return mentorsCanTeach; + const getPeopleOfClass = (className) => { - // TODO complete this function + + const studentsInClass = students.filter(student => student.class === className) + .map(student => student.name); + + const moduleName = classes.filter(whatClassIs => whatClassIs.name === className) + .map(module => module.currentModule)[0]; + + const mentorName = mentors.filter(mod => mod.nowTeaching === moduleName) + .map(TeacherName => TeacherName.name)[0]; + + const listOfNames = [ + { + name: studentsInClass[0], + role: 'student' + }, + { + name: mentorName, + role: 'mentor' + + }, + ]; + return listOfNames; }; // You can uncomment out this line to try your function // console.log(getPeopleOfClass('class34')); @@ -29,8 +58,16 @@ const getPeopleOfClass = (className) => { * class35: [{ name: 'Jane', role: 'student' }, { name: 'Steve', role: 'mentor' }] * } */ + const getActiveClasses = () => { - // TODO complete this function + const activeClasses = classes.filter(cls => cls.active); + + const result = {}; + + activeClasses.forEach(element => { + result[element.name] = getPeopleOfClass(element.name); + }); + + return result; }; -// You can uncomment out this line to try your function -// console.log(getActiveClasses()); +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..12080dd 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -3,10 +3,14 @@ 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() { @@ -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.#dailyAllowance += amount; return amount; } @@ -37,11 +46,22 @@ class Wallet { wallet.deposit(withdrawnAmount); } - reportBalance() { + setDailyAllowance(newAllowance) { + this.#dailyAllowance = newAllowance; console.log( - `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` ); } + + resetDailyAllowance() { + this.#dailyAllowance = 0; + } + + reportBalance() { + console.log( + `Name: ${this.#name}, balance: ${eurosFormatter.format(this.#cash)}`); + } + } function main() { diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..85c3d51 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -4,6 +4,12 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, + + getName: function () { + return this._name; + }, deposit: function (amount) { this._cash += amount; @@ -14,8 +20,14 @@ 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; }, @@ -26,18 +38,29 @@ function createWallet(name, cash = 0) { } to ${wallet.getName()}` ); const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); + if (withdrawnAmount > 0) { + wallet.deposit(withdrawnAmount); + } + }, + + setDailyAllowance: function (newAllowance = 40) { + 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)}` ); }, - getName: function () { - return this._name; - }, }; } @@ -55,6 +78,9 @@ function main() { walletJack.reportBalance(); walletJoe.reportBalance(); walletJane.reportBalance(); + walletJack.resetDailyAllowance(); + walletJoe.setDailyAllowance(50); + walletJane.setDailyAllowance(); } main(); 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..3e95419 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -10,7 +10,13 @@ function withdraw(amount) { return 0; } + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; } @@ -34,15 +40,28 @@ function getName() { return this._name; } +function resetDailyAllowance() { + this._dayTotalWithdrawals = 0; +} + +function setDailyAllowance(newAllowance = 40) { + this._dailyAllowance = newAllowance; + console.log(`Daily allowance updated to ${newAllowance}.`); +} + function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit, withdraw, transferInto, reportBalance, getName, + resetDailyAllowance, + setDailyAllowance, }; } @@ -60,6 +79,9 @@ function main() { walletJack.reportBalance(); walletJoe.reportBalance(); walletJane.reportBalance(); + walletJack.resetDailyAllowance(); + walletJoe.setDailyAllowance(60); + walletJane.setDailyAllowance(); } main(); diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..293d428 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._dayTotalWithdrawals = 0; } Wallet.prototype.deposit = function (amount) { @@ -15,7 +17,13 @@ Wallet.prototype.withdraw = function (amount) { return 0; } + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; }; @@ -28,6 +36,9 @@ Wallet.prototype.transferInto = function (wallet, amount) { const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); }; +Wallet.prototype.resetDailyAllowance = function () { + this._dayTotalWithdrawals = 0; +}; Wallet.prototype.reportBalance = function () { console.log( @@ -35,6 +46,12 @@ Wallet.prototype.reportBalance = function () { ); }; +Wallet.prototype.setDailyAllowance = function (newAllowance = 40) { + this._dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`); +}; + + Wallet.prototype.getName = function () { return this._name; }; @@ -53,6 +70,9 @@ function main() { walletJack.reportBalance(); walletJoe.reportBalance(); walletJane.reportBalance(); + + walletJack.resetDailyAllowance(); + walletJack.withdraw(60); } main();