From 2893542d103b0e70a6314b94ce6b9cb92a60a5eb Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Fri, 3 Nov 2023 18:09:28 +0100 Subject: [PATCH 01/17] Update traffic-light-1.js --- Week1/prep-exercises/1-traffic-light/traffic-light-1.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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..d13e6b4 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -11,7 +11,10 @@ let rotations = 0; while (rotations < 2) { const currentState = trafficLight.state; console.log("The traffic light is on", currentState); - +if (trafficLight.state == "green"){ trafficLight.state = "orange"}; +else if(trafficLight.state == "orange"){trafficLight.state = "red"}; +else if((trafficLight.state == "red"){trafficLight.state = "green"}; +rotations++; // TODO // if the color is green, turn it orange // if the color is orange, turn it red From c940f0e5b14de595b055f4aa1668bf6950e786d7 Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Fri, 3 Nov 2023 18:10:02 +0100 Subject: [PATCH 02/17] Update traffic-light-1.js --- Week1/prep-exercises/1-traffic-light/traffic-light-1.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d13e6b4..5ed749e 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -13,7 +13,7 @@ while (rotations < 2) { console.log("The traffic light is on", currentState); if (trafficLight.state == "green"){ trafficLight.state = "orange"}; else if(trafficLight.state == "orange"){trafficLight.state = "red"}; -else if((trafficLight.state == "red"){trafficLight.state = "green"}; +else if(trafficLight.state == "red"){trafficLight.state = "green"}; rotations++; // TODO // if the color is green, turn it orange From 25b64b7a1f4e475792be2426ef7c63ce44256261 Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Fri, 3 Nov 2023 18:26:44 +0100 Subject: [PATCH 03/17] Update traffic-light-2.js --- Week1/prep-exercises/1-traffic-light/traffic-light-2.js | 6 ++++++ 1 file changed, 6 insertions(+) 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..78d6c11 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -13,6 +13,12 @@ let cycle = 0; while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); + if(currentState == "green"){trafficLight.stateIndex = 1}; + else if (currentState == "orange"){trafficLight.stateIndex = 2}; + else (currentState == "red"){cycle++}; + + + } // TODO // if the color is green, turn it orange From f141adf987ed91b75a3c3b582f60417a717c5f4e Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Fri, 3 Nov 2023 18:51:58 +0100 Subject: [PATCH 04/17] Update traffic-light-2.js --- Week1/prep-exercises/1-traffic-light/traffic-light-2.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 78d6c11..f85c8c8 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -13,9 +13,9 @@ let cycle = 0; while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); - if(currentState == "green"){trafficLight.stateIndex = 1}; - else if (currentState == "orange"){trafficLight.stateIndex = 2}; - else (currentState == "red"){cycle++}; + if(currentState === "green"){trafficLight.stateIndex = 1}; + else if (currentState === "orange"){trafficLight.stateIndex = 2}; + else (currentState === "red"){trafficLight.stateIndex = 2 && cycle++}; } From 0d2ce2b35913f6344680cf5a4e96fe1ad01b54e2 Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Fri, 3 Nov 2023 18:52:35 +0100 Subject: [PATCH 05/17] Update traffic-light-1.js --- Week1/prep-exercises/1-traffic-light/traffic-light-1.js | 6 +++--- 1 file changed, 3 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 5ed749e..a40ba52 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -11,9 +11,9 @@ let rotations = 0; while (rotations < 2) { const currentState = trafficLight.state; console.log("The traffic light is on", currentState); -if (trafficLight.state == "green"){ trafficLight.state = "orange"}; -else if(trafficLight.state == "orange"){trafficLight.state = "red"}; -else if(trafficLight.state == "red"){trafficLight.state = "green"}; +if (trafficLight.state === "green"){ trafficLight.state = "orange"}; +else if(trafficLight.state === "orange"){trafficLight.state = "red"}; +else if(trafficLight.state === "red"){trafficLight.state = "green"}; rotations++; // TODO // if the color is green, turn it orange From f2fd094060b294f5be591d315306dbb4ea03a3bb Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Fri, 3 Nov 2023 18:58:25 +0100 Subject: [PATCH 06/17] Update README.md --- Week1/prep-exercises/1-traffic-light/README.md | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Week1/prep-exercises/1-traffic-light/README.md b/Week1/prep-exercises/1-traffic-light/README.md index d994741..8deecbf 100644 --- a/Week1/prep-exercises/1-traffic-light/README.md +++ b/Week1/prep-exercises/1-traffic-light/README.md @@ -1,9 +1,4 @@ -# Prep exercise - traffic light +you can finf the solutions in the follow links: -Let's have a deeper look at the working of traffic lights this week so that we can practice logic and loops. In `traffic-light-1.js` and `traffic-light-2.js` you will find the same requirements but with different ways of representing the traffic light. Have a look through the files and solve them so you can see how the way we represent data affects the way we need to solve problems. - -## Things to think about - -- Which way of representing the traffic light did you find better? Why? -- What happens if you change the loop to a `do-while` loop instead of a `while` loop? Why? -- We could have also used a `for` loop to make the traffic light do 2 full rotations. Do you think that would be better? Why or why not? +1-[traffic-light-1.js](traffic-light-1.js) +2-[traffic-light-2.js](traffic-light-2.js) From 9fd590b3f7ec269a78cb1e0256aeee1b6c228274 Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Fri, 3 Nov 2023 18:59:00 +0100 Subject: [PATCH 07/17] Update README.md --- Week1/prep-exercises/1-traffic-light/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Week1/prep-exercises/1-traffic-light/README.md b/Week1/prep-exercises/1-traffic-light/README.md index 8deecbf..3df88ca 100644 --- a/Week1/prep-exercises/1-traffic-light/README.md +++ b/Week1/prep-exercises/1-traffic-light/README.md @@ -1,4 +1,6 @@ you can finf the solutions in the follow links: 1-[traffic-light-1.js](traffic-light-1.js) + + 2-[traffic-light-2.js](traffic-light-2.js) From 769825957888826fa0a991f8833c07a2e95ba052 Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Fri, 3 Nov 2023 18:59:30 +0100 Subject: [PATCH 08/17] Update README.md --- Week1/prep-exercises/1-traffic-light/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week1/prep-exercises/1-traffic-light/README.md b/Week1/prep-exercises/1-traffic-light/README.md index 3df88ca..47d07fc 100644 --- a/Week1/prep-exercises/1-traffic-light/README.md +++ b/Week1/prep-exercises/1-traffic-light/README.md @@ -1,4 +1,4 @@ -you can finf the solutions in the follow links: +you can find the solutions in the follow links: 1-[traffic-light-1.js](traffic-light-1.js) From 9567a94af20985797940dd17c3c6db162e9afba4 Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Fri, 3 Nov 2023 19:01:23 +0100 Subject: [PATCH 09/17] Update traffic-light-2.js --- Week1/prep-exercises/1-traffic-light/traffic-light-2.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 f85c8c8..3150bfa 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -15,7 +15,8 @@ while (cycle < 2) { console.log("The traffic light is on", currentState); if(currentState === "green"){trafficLight.stateIndex = 1}; else if (currentState === "orange"){trafficLight.stateIndex = 2}; - else (currentState === "red"){trafficLight.stateIndex = 2 && cycle++}; + else (currentState === "red"){trafficLight.stateIndex = 0}; + cycle++; } From 82a4d128b525229fbf0cc9cd7566e6228ae302fa Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Fri, 3 Nov 2023 19:51:09 +0100 Subject: [PATCH 10/17] Update traffic-light-1.js --- .../1-traffic-light/traffic-light-1.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) 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 a40ba52..7fb94b7 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -15,6 +15,28 @@ if (trafficLight.state === "green"){ trafficLight.state = "orange"}; else if(trafficLight.state === "orange"){trafficLight.state = "red"}; else if(trafficLight.state === "red"){trafficLight.state = "green"}; rotations++; + + second method: + switch (currentState) { + case "greeen": + trafficlight.state = "orange"; + break; +} + switch (currentState) { + case "orange": + trafficlight.state = "red"; + break; + } + switch (currentState) { + case "red": + trafficlight.state = "green"; + rotations++; + break; + + default: + break; + + // TODO // if the color is green, turn it orange // if the color is orange, turn it red From b6a37699439fa58d15cdc2923a69726b1ca0f90f Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Fri, 10 Nov 2023 12:10:53 +0100 Subject: [PATCH 11/17] Update traffic-light.js --- .../1-traffic-light/traffic-light.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..bd031d0 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -9,14 +9,30 @@ 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]; } function getNextStateIndex(trafficLight) { + const currentStateIndex = trafficLight.stateIndex; // 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 + var nextStateIndex; + + switch (currentStateIndex) { + case 0: // Green + nextStateIndex = 1; + break; + case 1: // Orange + nextStateIndex = 2; + break; + case 2: // Red + nextStateIndex = 0; + break; + default: + console.error("Unexpected state index:", currentStateIndex); } // This function loops for the number of seconds specified by the `secs` From 66dcaea2400fdf2da10f78d2afb0d267589f82d2 Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Fri, 17 Nov 2023 17:30:51 +0100 Subject: [PATCH 12/17] Update 1-find-mentors.js --- Week3/prep-exercises/1-hyf-program/1-find-mentors.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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..e5e423e 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -9,9 +9,11 @@ import { modules, students, mentors, classes } from "./hyf.js"; */ const possibleMentorsForModule = (moduleName) => { // TODO complete this function + const theMentors = mentors.filter((mentor) => mentors.ableToTeach.includes(moduleName) > 0); + return theMentors.map((mentor) => mentor.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. @@ -21,6 +23,11 @@ const possibleMentorsForModule = (moduleName) => { */ const findMentorForModule = (moduleName) => { // TODO complete this function + const theMentors = mentors.filter((mentor) => mentors.ableToTeach.includes(moduleName) > 0); + if (theMentors.length > 0){ + const theWantedMentors = math.floor(math.random() * theMentors.length > 0); + return theMentors[theWantedMentors]; + } }; // You can uncomment out this line to try your function -// console.log(findMentorForModule('javascript')); + console.log(findMentorForModule('javascript')); From dca07a5da0ae091c4f9a3ee11ce23ffb8651dad0 Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Sat, 18 Nov 2023 18:17:06 +0100 Subject: [PATCH 13/17] Update 2-class-list.js --- .../1-hyf-program/2-class-list.js | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) 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..dac5b83 100644 --- a/Week3/prep-exercises/1-hyf-program/2-class-list.js +++ b/Week3/prep-exercises/1-hyf-program/2-class-list.js @@ -12,17 +12,49 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function + const classInfo = classes.find((class) => class.name === className); + + if (!classInfo) { + + return []; + } + + const studentsInClass = classInfo.students.map((studentN) => { + + + const student = students.find((stud) => stud.id === studentId); + + return { name: student.name, role: 'student' }; + }); + + const mentorsInClass = classInfo.mentors + .map((mentorId) => { + const mentor = mentors.find((m) => m.id === mentorId); + + const currentModule = modules.find( + (module) => module.name === classInfo.currentModule + ); + if (mentor.nowTeaching === currentModule.name) { + + return { name: mentor.name, role: 'mentor' }; + } + return null; + }) + .filter((mentor) => mentor !== null); + + return [...studentsInClass, ...mentorsInClass]; }; + // 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. * 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: + * 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' }], @@ -30,7 +62,18 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - // TODO complete this function + const activeClasses = classes.filter((class) => class.isActive); + + const result = {}; + activeClasses.forEach((class) => { + + const peopleInClass = getPeopleOfClass(class.name); + + result[class.name] = peopleInClass; + }); + + return result; }; + // You can uncomment out this line to try your function -// console.log(getActiveClasses()); +console.log(getActiveClasses()); From 455e997d12389c4cde2527ebbe132fb4872ec5d2 Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Sat, 25 Nov 2023 08:08:30 +0100 Subject: [PATCH 14/17] Update ex2-classes.js --- Week4/prep-exercises/1-wallet/ex2-classes.js | 23 ++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..8cd78dc 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -1,12 +1,14 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from "./euroFormatter.js"; class Wallet { #name; #cash; + dailyAllowance; constructor(name, cash) { this.#name = name; this.#cash = cash; + this.dailyAllowance = 40; } get name() { @@ -23,6 +25,11 @@ class Wallet { return 0; } + if (amount > this.dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } + this.#cash -= amount; return amount; } @@ -37,6 +44,13 @@ class Wallet { wallet.deposit(withdrawnAmount); } + 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)}` @@ -45,11 +59,12 @@ class Wallet { } 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); walletJane.transferInto(walletJoe, 25); walletJane.deposit(20); From 9360bc651704efc32d63b7557480e00333d6b982 Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Sat, 25 Nov 2023 08:08:53 +0100 Subject: [PATCH 15/17] Update ex3-object.js --- Week4/prep-exercises/1-wallet/ex3-object.js | 22 ++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..0d129fa 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,9 +1,10 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from "./euroFormatter.js"; function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, deposit: function (amount) { this._cash += amount; @@ -14,7 +15,10 @@ function createWallet(name, cash = 0) { console.log(`Insufficient funds!`); return 0; } - + if (amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } this._cash -= amount; return amount; }, @@ -28,6 +32,12 @@ function createWallet(name, cash = 0) { const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); }, + setDailyAllowance: function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + }, reportBalance: function () { console.log( @@ -42,9 +52,11 @@ function createWallet(name, cash = 0) { } 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.transferInto(walletJoe, 50); walletJane.transferInto(walletJoe, 25); From 1b3eae429f0c83f8519f97e8cc385e7c383c1399 Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Sat, 25 Nov 2023 08:09:09 +0100 Subject: [PATCH 16/17] Update ex4-object-shared-methods.js From 59dc4088d1ffa761ec28a90bd872b1e3b0b9b560 Mon Sep 17 00:00:00 2001 From: Bilal Zalkhi <148480265+Bilalzalkhi@users.noreply.github.com> Date: Sat, 25 Nov 2023 08:09:35 +0100 Subject: [PATCH 17/17] Update ex5-prototype.js --- .../prep-exercises/1-wallet/ex5-prototype.js | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..35faeca 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -1,8 +1,9 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from "./euroFormatter.js"; function Wallet(name, cash) { this._name = name; this._cash = cash; + this._dailyAllowance = 40; } Wallet.prototype.deposit = function (amount) { @@ -14,7 +15,10 @@ Wallet.prototype.withdraw = function (amount) { console.log(`Insufficient funds!`); return 0; } - + if (amount > this._dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } this._cash -= amount; return amount; }; @@ -25,6 +29,12 @@ Wallet.prototype.transferInto = function (wallet, amount) { this._name } to ${wallet.getName()}` ); + Wallet.prototype.setDailyAllowance = function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + }; const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); }; @@ -40,10 +50,12 @@ Wallet.prototype.getName = function () { }; 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);