From df72884c7bc830ac58ffeb7e12c46406c9bed766 Mon Sep 17 00:00:00 2001 From: Mariam Isaeva Date: Fri, 10 Nov 2023 18:47:11 +0100 Subject: [PATCH 1/5] ex1,2 --- .../1-traffic-light/traffic-light.js | 8 +++++-- Week2/prep-exercises/2-experiments/index.js | 21 +++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..683f59d 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -6,17 +6,21 @@ */ 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]; //return array[index] } 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 + let nextIndex = trafficLight.stateIndex +1; //declare nextIndex = index+1 + if (nextIndex>=trafficLight.possibleStates.length){ //if indexNum is greater than array's length + nextIndex=0; //assign it to 0 + } + return nextIndex; } // This function loops for the number of seconds specified by the `secs` diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..c55b3f3 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -3,7 +3,10 @@ function runExperiment(sampleSize) { const valueCounts = [0, 0, 0, 0, 0, 0]; - // TODO + for (let i=0; i Date: Fri, 10 Nov 2023 19:13:25 +0100 Subject: [PATCH 2/5] add i --- .DS_Store | Bin 6148 -> 6148 bytes .gitignore | 1 + Week2/prep-exercises/2-experiments/index.js | 2 +- 3 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.DS_Store b/.DS_Store index 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..fe2bbd6aed33a9d8c28ee3a1f8d1169afc840d75 100644 GIT binary patch literal 6148 zcmeHKO>fgM7=GPlZNot7fl2HZ32`f<-Gb1>C6w;Kfy;un10N+JRU0jfrzTwvRi#KA z;kR(-k8tOgz~hf?P1jwgYVsS|kNtkdek{i^5wYGh>JqhxsEH!bdVp$$2)igZq^30w zflTChm+)_4!b9{$LL-I&!@znoAoi|ByELMh^6LJr9mDqQpAZt=V?%e^r9r3JsX>@U$Gs9sHrTlJJw|6QOOfom>$Yc#jke4J)@ zh5^IC-7_G*4-SezPh+W2emYReD*(`fS_<0o*B|Hz1<=!2Duf2YR47n|GJVBh`sTn? zIP9MGFBPhAV)D(%quAjHu~2NHo+2v)5HM~`T*bUufrFi8Gb@K62T*deAjfy+$^0UUjEs{_ QMU*G|^KfjA5m~_u0KiTTI{*Lx diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..496ee2c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store \ No newline at end of file diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index c55b3f3..42e1170 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -37,7 +37,7 @@ function main() { const sampleSizes = [100, 1000, 1000000]; for (let i of sampleSizes){ - console.log(runExperiment(i)); + console.log(runExperiment(i), i); } // Write a for..of loop that calls the `runExperiment()` function for each // value of the `sampleSizes` array. From 439e9a9e9f5c52bca03890f630e8431acf970e68 Mon Sep 17 00:00:00 2001 From: Mariam Isaeva Date: Fri, 10 Nov 2023 19:21:32 +0100 Subject: [PATCH 3/5] moved week1 --- .../1-traffic-light/traffic-light-1.js | 15 +++++++++++++-- .../1-traffic-light/traffic-light-2.js | 13 ++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) 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..5d27c52 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -11,12 +11,23 @@ let rotations = 0; while (rotations < 2) { const currentState = trafficLight.state; console.log("The traffic light is on", currentState); - + switch (currentState) { + case 'green': + trafficLight.state = 'orange'; + break; + case 'orange': + trafficLight.state = 'red'; + break; + case 'red': + trafficLight.state = 'green'; + rotations++; + } +} // TODO // 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 -} + /** * The output should be: 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..090cfa5 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -14,11 +14,22 @@ while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); + for (let i = trafficLight.stateIndex + 1; i < trafficLight.possibleStates.length; i++) { + + if (trafficLight.possibleStates[i] == 'green' || trafficLight.possibleStates[i] == 'orange') { + trafficLight.possibleStates[i + 1]; + } + console.log("The traffic light is on", trafficLight.possibleStates[i]) + } + + cycle++; +} + // TODO // 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 -} + /** * The output should be: From dc2ab77e8ddc96618fbb94049d2bb36448324b83 Mon Sep 17 00:00:00 2001 From: Mariam Isaeva Date: Fri, 17 Nov 2023 20:00:06 +0100 Subject: [PATCH 4/5] Done 1,2 --- .../.PVS-Studio/State/WindowState.json | 24 +++ .../1-hyf-program/1-find-mentors.js | 18 +- .../1-hyf-program/2-class-list.js | 29 +++- Week3/prep-exercises/1-hyf-program/hyf.js | 162 ++++++++++-------- 4 files changed, 151 insertions(+), 82 deletions(-) create mode 100644 Week3/prep-exercises/1-hyf-program/.PVS-Studio/State/WindowState.json diff --git a/Week3/prep-exercises/1-hyf-program/.PVS-Studio/State/WindowState.json b/Week3/prep-exercises/1-hyf-program/.PVS-Studio/State/WindowState.json new file mode 100644 index 0000000..fd88c78 --- /dev/null +++ b/Week3/prep-exercises/1-hyf-program/.PVS-Studio/State/WindowState.json @@ -0,0 +1,24 @@ +{ + "isDataChanged": false, + "filters": { + "code": "", + "message": "", + "files": "", + "level": [ + 0, + 1, + 2, + 3 + ], + "isColumnFiltersVisible": true, + "isLevelFiltersVisible": true, + "isGroupFiltersVisible": true, + "type": [ + "General", + "Optimization" + ] + }, + "loadableProcessIds": [ + "loadState" + ] +} \ 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..cbdd705 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -7,11 +7,13 @@ import { modules, students, mentors, classes } from "./hyf.js"; * It should return an array of names. So something like: * ['John', 'Mary'] */ -const possibleMentorsForModule = (moduleName) => { - // TODO complete this function +const possibleMentorsForModule = (moduleName) => {//who's from mentors canTeach the provided(included) moduleName + const availableMentors = mentors.filter(m => m.canTeach.includes(moduleName)) + //extract the names which match with previous condition + return availableMentors.map(el => el.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. @@ -20,7 +22,13 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const modMentors = possibleMentorsForModule(moduleName); + + if (modMentors.length > 0) { + const random = Math.floor(Math.random() * modMentors.length); + return modMentors[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..9255e6e 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,21 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function + //find 'current module' from classes + const mod = classes.find(cl => cl.name === className).currentModule; //or mod.currentModule + + //return new Arr + return [ + ...students.filter(st => st.class === className) + .map((st) => ({ name: st.name, role: 'student' })), + ...mentors.filter(m => m.nowTeaching === mod) + .map(m => ({ name: m.name, role: 'mentor' })) + ]; + }; + // 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 +41,17 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - // TODO complete this function + const newObj = {}; + + const activeClass = classes.filter(a => a.active) + + activeClass.forEach(ac => { + const peopleFunc = getPeopleOfClass(ac.name); + + return newObj[ac.name] = peopleFunc; //obj = arr of objs + }); + return newObj; }; // You can uncomment out this line to try your function -// console.log(getActiveClasses()); +console.log(getActiveClasses()); +//getActiveClasses(); \ No newline at end of file diff --git a/Week3/prep-exercises/1-hyf-program/hyf.js b/Week3/prep-exercises/1-hyf-program/hyf.js index c06c02c..a4dd372 100644 --- a/Week3/prep-exercises/1-hyf-program/hyf.js +++ b/Week3/prep-exercises/1-hyf-program/hyf.js @@ -1,84 +1,100 @@ export const modules = [ - { name: "html-css", displayName: "HTML/CSS" }, - { name: "javascript", displayName: "JavaScript" }, - { name: "browsers", displayName: "Browsers" }, - { name: "using-apis", displayName: "Using APIs" }, - { name: "node", displayName: "Node.js" }, - { name: "databases", displayName: "Databases" }, - { name: "react", displayName: "React" }, - { name: "project", displayName: "Project" }, + { name: "html-css", displayName: "HTML/CSS" }, + { name: "javascript", displayName: "JavaScript" }, + { name: "browsers", displayName: "Browsers" }, + { name: "using-apis", displayName: "Using APIs" }, + { name: "node", displayName: "Node.js" }, + { name: "databases", displayName: "Databases" }, + { name: "react", displayName: "React" }, + { name: "project", displayName: "Project" }, ]; export const classes = [ - { - name: "class32", - startDate: "23-3-2021", - active: false, - graduationDate: "7-11-2021", - }, - { - name: "class33", - startDate: "28-5-2021", - active: false, - graduationDate: "7-11-2021", - }, - { - name: "class34", - startDate: "2-9-2021", - active: true, - currentModule: "react", - }, - { - name: "class35", - startDate: "14-11-2021", - active: true, - currentModule: "using-apis", - }, - { - name: "class36", - startDate: "5-1-2022", - active: true, - currentModule: "javascript", - }, + { + name: "class32", + startDate: "23-3-2021", + active: false, + graduationDate: "7-11-2021", + }, + { + name: "class33", + startDate: "28-5-2021", + active: false, + graduationDate: "7-11-2021", + }, + { + name: "class34", + startDate: "2-9-2021", + active: true, + currentModule: "react", + }, + { + name: "class35", + startDate: "14-11-2021", + active: true, + currentModule: "using-apis", + }, + { + name: "class36", + startDate: "5-1-2022", + active: true, + currentModule: "javascript", + }, ]; export const students = [ - { name: "Fede", class: "class33", gitHubName: "fedefu", graduated: false }, - { name: "Tjebbe", class: "class32", gitHubName: "Tjebbee", graduated: true }, - { name: "Rob", class: "class34", gitHubName: "robvk", graduated: false }, - { - name: "Wouter", - class: "class35", - gitHubName: "wouterkleijn", - graduated: false, - }, + { + name: "Fede", + class: "class33", + gitHubName: "fedefu", + graduated: false + }, + + { + name: "Tjebbe", + class: "class32", + gitHubName: "Tjebbee", + graduated: true + }, + { + name: "Rob", + class: "class34", + gitHubName: "robvk", + graduated: false + }, + { + name: "Wouter", + class: "class35", + gitHubName: "wouterkleijn", + graduated: false, + }, ]; export const mentors = [ - { - name: "Stas", - canTeach: ["javascript", "browsers", "using-apis"], - nowTeaching: "javascript", - }, - { - name: "Andrej", - canTeach: ["using-apis", "node"], - }, - { - name: "Shriyans", - canTeach: ["react"], - nowTeaching: "react", - }, - { - name: "Yash", - canTeach: ["javascript", "using-apis"], - }, - { - name: "Rohan", - canTeach: ["html/css/git", "javascript", "node"], - }, - { - name: "Collin", - canTeach: ["browsers", "using-apis", "node"], - }, + { + name: "Stas", + canTeach: ["javascript", "browsers", "using-apis"], + nowTeaching: "javascript", + }, + { + name: "Andrej", + canTeach: ["using-apis", "node"], + }, + { + name: "Shriyans", + canTeach: ["react"], + nowTeaching: "react", + }, + { + name: "Yash", + canTeach: ["javascript", "using-apis"], + }, + { + name: "Rohan", + canTeach: ["html/css/git", "javascript", "node"], + }, + { + name: "Collin", + canTeach: ["browsers", "using-apis", "node"], + }, ]; From a21408d7bbb8acd8ec6a262f68067027289dd70f Mon Sep 17 00:00:00 2001 From: Mariam Isaeva Date: Sat, 25 Nov 2023 17:25:23 +0100 Subject: [PATCH 5/5] Done prep-w4 --- Week4/prep-exercises/1-wallet/ex2-classes.js | 123 ++++++++++-------- Week4/prep-exercises/1-wallet/ex3-object.js | 116 ++++++++++------- .../1-wallet/ex4-object-shared-methods.js | 96 ++++++++------ .../prep-exercises/1-wallet/ex5-prototype.js | 78 ++++++----- Week4/prep-exercises/1-wallet/package.json | 13 ++ 5 files changed, 260 insertions(+), 166 deletions(-) create mode 100644 Week4/prep-exercises/1-wallet/package.json diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..f561ee6 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -1,63 +1,84 @@ import eurosFormatter from './euroFormatter.js'; class Wallet { - #name; - #cash; - - constructor(name, cash) { - this.#name = name; - this.#cash = cash; - } - - get name() { - return this.#name; - } - - deposit(amount) { - this.#cash += amount; - } - - withdraw(amount) { - if (this.#cash - amount < 0) { - console.log(`Insufficient funds!`); - return 0; - } - - this.#cash -= amount; - return amount; - } - - transferInto(wallet, amount) { - console.log( - `Transferring ${eurosFormatter.format(amount)} from ${this.name} to ${ - wallet.name - }` - ); - const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); - } - - reportBalance() { - console.log( - `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` - ); - } + #name; + #cash; + + constructor(name, cash = 0) { + this.#name = name; + this.#cash = cash; + this.dailyAllowance = 40; + this.dayTotalWithdrawals = 0; + } + + get name() { + return this.#name; + } + + deposit(amount) { + this.#cash += amount; + } + + withdraw(amount) { + if (this.#cash - amount < 0) { + console.log(`Insufficient funds!`); + return 0; + } + + if (this.dayTotalWithdrawals + amount > this.dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + + this.#cash -= amount; + return amount; + } + + transferInto(wallet, amount) { + console.log( + `Transferring ${eurosFormatter.format(amount)} from ${this.name} to ${wallet.name + }` + ); + const withdrawnAmount = this.withdraw(amount); + wallet.deposit(withdrawnAmount); + } + + setDailyAllowance(newAllowance) { + this.dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + } + + resetDailyAllowance() { + this.dayTotalWithdrawals = 0; + } + + + reportBalance() { + console.log( + `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` + ); + } } 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.transferInto(walletJoe, 25); - walletJane.deposit(20); - walletJane.transferInto(walletJoe, 25); + walletJane.deposit(20); + walletJane.transferInto(walletJoe, 25); - walletJack.reportBalance(); - walletJoe.reportBalance(); - walletJane.reportBalance(); + walletJack.reportBalance(); + walletJoe.reportBalance(); + walletJane.reportBalance(); } main(); diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..4a319c1 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,60 +1,80 @@ import eurosFormatter from './euroFormatter.js'; function createWallet(name, cash = 0) { - return { - _name: name, - _cash: cash, - - deposit: function (amount) { - this._cash += amount; - }, - - withdraw: function (amount) { - if (this._cash - amount < 0) { - console.log(`Insufficient funds!`); - return 0; - } - - this._cash -= amount; - return amount; - }, - - transferInto: function (wallet, amount) { - console.log( - `Transferring ${eurosFormatter.format(amount)} from ${ - this._name - } to ${wallet.getName()}` - ); - const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); - }, - - reportBalance: function () { - console.log( - `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` - ); - }, - - getName: function () { - return this._name; - }, - }; + return { + _name: name, + _cash: cash, + dailyAllowance: 40, + dayTotalWithdrawals: 0, + + deposit: function (amount) { + this._cash += amount; + }, + + withdraw: function (amount) { + if (this._cash - amount < 0) { + console.log(`Insufficient funds!`); + return 0; + } + + if (this.dayTotalWithdrawals + amount > this.dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + + this._cash -= amount; + return amount; + }, + + transferInto: function (wallet, amount) { + console.log( + `Transferring ${eurosFormatter.format(amount)} from ${this._name + } to ${wallet.getName()}` + ); + const withdrawnAmount = this.withdraw(amount); + wallet.deposit(withdrawnAmount); + }, + + setDailyAllowance: function (newAllowance) { + this.dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + }, + + resetDailyAllowance: function () { + dayTotalWithdrawals = 0; + }, + + reportBalance: function () { + console.log( + `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` + ); + }, + + getName: function () { + return this._name; + }, + }; } 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.setDailyAllowance(80); - walletJack.transferInto(walletJoe, 50); - walletJane.transferInto(walletJoe, 25); + walletJane.transferInto(walletJoe, 25); - walletJane.deposit(20); - walletJane.transferInto(walletJoe, 25); + walletJane.deposit(20); + walletJane.transferInto(walletJoe, 25); - walletJack.reportBalance(); - walletJoe.reportBalance(); - walletJane.reportBalance(); + walletJack.reportBalance(); + walletJoe.reportBalance(); + walletJane.reportBalance(); } 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..f9ed0d1 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -1,65 +1,87 @@ import eurosFormatter from './euroFormatter.js'; function deposit(amount) { - this._cash += amount; + this._cash += amount; } function withdraw(amount) { - if (this._cash - amount < 0) { - console.log(`Insufficient funds!`); - return 0; - } + if (this._cash - amount < 0) { + console.log(`Insufficient funds!`); + return 0; + } - this._cash -= amount; - return amount; + if (this.dayTotalWithdrawals + amount > this.dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + + this._cash -= amount; + return amount; } function transferInto(wallet, amount) { - console.log( - `Transferring ${eurosFormatter.format(amount)} from ${ - this._name - } to ${wallet.getName()}` - ); - const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); + console.log( + `Transferring ${eurosFormatter.format(amount)} from ${this._name + } to ${wallet.getName()}` + ); + const withdrawnAmount = this.withdraw(amount); + wallet.deposit(withdrawnAmount); +} + +function setDailyAllowance(newAllowance) { + this.dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); } + +function resetDailyAllowance() { + this.dayTotalWithdrawals = 0; +} + + function reportBalance() { - console.log( - `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` - ); + console.log( + `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` + ); } function getName() { - return this._name; + return this._name; } function createWallet(name, cash = 0) { - return { - _name: name, - _cash: cash, - deposit, - withdraw, - transferInto, - reportBalance, - getName, - }; + return { + _name: name, + _cash: cash, + dailyAllowance: 40, + dayTotalWithdrawals: 0, + deposit, + withdraw, + transferInto, + setDailyAllowance, + resetDailyAllowance, + reportBalance, + getName, + }; } 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); - walletJane.transferInto(walletJoe, 25); + walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80); + walletJane.transferInto(walletJoe, 25); - walletJane.deposit(20); - walletJane.transferInto(walletJoe, 25); + walletJane.deposit(20); + walletJane.transferInto(walletJoe, 25); - walletJack.reportBalance(); - walletJoe.reportBalance(); - walletJane.reportBalance(); + walletJack.reportBalance(); + walletJoe.reportBalance(); + walletJane.reportBalance(); } main(); diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..71571a8 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -1,58 +1,76 @@ import eurosFormatter from './euroFormatter.js'; function Wallet(name, cash) { - this._name = name; - this._cash = cash; + this._name = name; + this._cash = cash; + this.dailyAllowance = 40; + this.dayTotalWithdrawals = 0; } Wallet.prototype.deposit = function (amount) { - this._cash += amount; + this._cash += amount; }; Wallet.prototype.withdraw = function (amount) { - if (this._cash - amount < 0) { - console.log(`Insufficient funds!`); - return 0; - } + if (this._cash - amount < 0) { + console.log(`Insufficient funds!`); + return 0; + } - this._cash -= amount; - return amount; + if (this.dayTotalWithdrawals + amount > this.dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + + this._cash -= amount; + return amount; }; Wallet.prototype.transferInto = function (wallet, amount) { - console.log( - `Transferring ${eurosFormatter.format(amount)} from ${ - this._name - } to ${wallet.getName()}` - ); - const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); + console.log( + `Transferring ${eurosFormatter.format(amount)} from ${this._name + } to ${wallet.getName()}` + ); + const withdrawnAmount = this.withdraw(amount); + wallet.deposit(withdrawnAmount); +}; + +Wallet.prototype.setDailyAllowance = function (newAllowance) { + this.dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); }; +Wallet.prototype.resetDailyAllowance = function () { + this.dayTotalWithdrawals = 0; +} + Wallet.prototype.reportBalance = function () { - console.log( - `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` - ); + console.log( + `Name: ${this._name}, balance: ${eurosFormatter.format(this._cash)}` + ); }; Wallet.prototype.getName = function () { - return this._name; + return this._name; }; 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); - walletJane.transferInto(walletJoe, 25); + walletJack.transferInto(walletJoe, 50); + walletJack.setDailyAllowance(80); + walletJane.transferInto(walletJoe, 25); - walletJane.deposit(20); - walletJane.transferInto(walletJoe, 25); + walletJane.deposit(20); + walletJane.transferInto(walletJoe, 25); - walletJack.reportBalance(); - walletJoe.reportBalance(); - walletJane.reportBalance(); + walletJack.reportBalance(); + walletJoe.reportBalance(); + walletJane.reportBalance(); } main(); diff --git a/Week4/prep-exercises/1-wallet/package.json b/Week4/prep-exercises/1-wallet/package.json new file mode 100644 index 0000000..263eb24 --- /dev/null +++ b/Week4/prep-exercises/1-wallet/package.json @@ -0,0 +1,13 @@ +{ + "name": "1-wallet", + "version": "1.0.0", + "description": "> Created by the one and only Jim, you can find him on our [Slack](https://hackyourfuture.slack.com/team/U383PTTK9) and on [GitHub](https://github.com/remarcmij)!", + "type": "module", + "main": "euroFormatter.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +} \ No newline at end of file