diff --git a/Week3/challenges/1-sum-entries.js b/Week3/challenges/1-sum-entries.js index f7dd419..c9dc9ed 100644 --- a/Week3/challenges/1-sum-entries.js +++ b/Week3/challenges/1-sum-entries.js @@ -8,10 +8,29 @@ Once you have found those numbers, multiply the numbers and store the result of const list = [1721, 979, 366, 299, 675, 1456]; -let result; - + // Write your code here +function findPair(list) { + const newSet = new Set(); + + for (let num of list) { + let complement = 2020 - num; + + if (newSet.has(complement)) { + return [num, complement]; + } + + newSet.add(num); + } + return null; +} + +let result = findPair(list).reduce((acc, curNum) => acc * curNum); + +console.log(result); + + // 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/prep-exercises/1-hyf-program/1-find-mentors.js b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js index 72baa61..09befc3 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 mentorsWhoCanTeach = mentors.filter(mentor => mentor.canTeach.includes(moduleName)); + const mentorsNames = mentorsWhoCanTeach.map((mentor => mentor.name)); + return mentorsNames; }; -// 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,9 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const mentorsNames = possibleMentorsForModule(moduleName); + const randomIndex = Math.floor(Math.random() * mentorsNames.length); + return mentorsNames[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..9da870e 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -11,11 +11,42 @@ import { modules, students, mentors, classes } from "./hyf.js"; * * [{ 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')); + + function filterByProperty (people, property, value) { + return people.filter((person) => { + const prop = person[property]; + if (Array.isArray(prop)) { + return prop.includes(value); + } else { + return prop === value; + } + }); + } + + function getPeopleInfo(people, role) { + return people.map((person) => ({ name: person.name, role: role})); + } + + const classStudents = filterByProperty(students, 'class', className); + const classStudentsInfo = getPeopleInfo(classStudents, 'student') + + const currentClass = classes.find(curClass => curClass.name === className); + if (!currentClass) { + console.error(`There is no ${currentClass} in the list`); + return []; + } + + const currentModuleOfClass = currentClass.currentModule; + + const currentMentor = filterByProperty(mentors, 'nowTeaching', currentModuleOfClass); + const currentMentorInfo = getPeopleInfo(currentMentor, 'mentor'); + + return classStudentsInfo.concat(currentMentorInfo); +} + +console.log(getPeopleOfClass('class34')); /** * We would like to have a complete overview of the current active classes. @@ -29,8 +60,15 @@ const getPeopleOfClass = (className) => { * class35: [{ name: 'Jane', role: 'student' }, { name: 'Steve', role: 'mentor' }] * } */ + const getActiveClasses = () => { - // TODO complete this function + return classes + .filter((activeClass) => + activeClass.active === true) + .reduce((result, curClass) => { + result[curClass.name] = getPeopleOfClass(curClass.name); + 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..474f70e 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -3,10 +3,15 @@ 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() { @@ -19,11 +24,16 @@ class Wallet { withdraw(amount) { if (this.#cash - amount < 0) { - console.log(`Insufficient funds!`); + console.log(`Insufficient funds! You have only ${eurosFormatter.format(this.#cash)}`); + return 0; + } + if (this.#dayTotalWithdrawals + amount > this.#dailyAllowance) { + console.log(`Insufficient remaining daily allowance! Your current daily allowance is ${eurosFormatter.format(this.#dailyAllowance)}`); return 0; } this.#cash -= amount; + this.#dayTotalWithdrawals += amount; return amount; } @@ -42,14 +52,28 @@ class Wallet { `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` ); } + + resetDailyAllowance() { + this.#dayTotalWithdrawals = 0; + } + + setDailyAllowance(newAllowance) { + this.#dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + } } 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); 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..1c961db 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -4,6 +4,8 @@ function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit: function (amount) { this._cash += amount; @@ -11,11 +13,15 @@ function createWallet(name, cash = 0) { withdraw: function (amount) { if (this._cash - amount < 0) { - console.log(`Insufficient funds!`); + console.log(`Insufficient funds! You have only ${eurosFormatter.format(this._cash)}`); + return 0; + } + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance! Your current daily allowance is ${eurosFormatter.format(this._dailyAllowance)}`); return 0; } - this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; }, @@ -38,6 +44,17 @@ function createWallet(name, cash = 0) { getName: function () { return this._name; }, + + resetDailyAllowance() { + this._dayTotalWithdrawals = 0; + }, + + setDailyAllowance(newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + } }; } @@ -47,6 +64,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.js index bd4fd20..e68a55d 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -6,10 +6,18 @@ function deposit(amount) { function withdraw(amount) { if (this._cash - amount < 0) { - console.log(`Insufficient funds!`); + console.log(`Insufficient funds! You have only ${eurosFormatter.format(this._cash)}`); return 0; } - + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log( + `Insufficient remaining daily allowance! Your current daily allowance is ${ + eurosFormatter.format(this._dailyAllowance)}` + ); + return 0; + } + + this._dayTotalWithdrawals += amount; this._cash -= amount; return amount; } @@ -17,10 +25,10 @@ function withdraw(amount) { function transferInto(wallet, amount) { console.log( `Transferring ${eurosFormatter.format(amount)} from ${ - this._name - } to ${wallet.getName()}` + this._name} to ${wallet.getName()}` ); const withdrawnAmount = this.withdraw(amount); + wallet.deposit(withdrawnAmount); } @@ -34,15 +42,30 @@ function getName() { return this._name; } +function resetDailyAllowance() { + this._dayTotalWithdrawals = 0; +} + +function setDailyAllowance(newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); +} + function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit, withdraw, transferInto, reportBalance, getName, + resetDailyAllowance, + setDailyAllowance }; } @@ -52,6 +75,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.js index 7cba410..b2c25d6 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) { @@ -14,7 +16,11 @@ Wallet.prototype.withdraw = function (amount) { console.log(`Insufficient funds!`); return 0; } - + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance! Your current daily allowance is ${eurosFormatter.format(this._dailyAllowance)}`); + return 0; + } + this._dayTotalWithdrawals += amount; this._cash -= amount; return amount; }; @@ -39,12 +45,26 @@ Wallet.prototype.getName = function () { return this._name; }; +Wallet.prototype.resetDailyAllowance = function() { + this._dayTotalWithdrawals = 0; +} + +Wallet.prototype.setDailyAllowance = function(newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); +} + function main() { 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); walletJane.deposit(20); @@ -56,3 +76,4 @@ function main() { } main(); + diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..e92b5b1 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "JavaScript", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..2810091 --- /dev/null +++ b/package.json @@ -0,0 +1,13 @@ +{ + "name": "javascript", + "version": "1.0.0", + "description": "> If you are following the HackYourFuture curriculum we recommend you to start with module 1: [HTML/CSS/GIT](https://github.com/HackYourFuture/HTML-CSS). To get a complete overview of the HackYourFuture curriculum first, click [here](https://github.com/HackYourFuture/curriculum).", + "main": "index.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +}