From 92fb94c021cc0cd6a1be334a5ccf719525ef165a Mon Sep 17 00:00:00 2001 From: Vladislav Nachikov Date: Sat, 10 May 2025 18:43:40 +0200 Subject: [PATCH 1/4] Homework from Week 1 js --- .../practice-exercises/1-remove-the-comma.js | 15 ++++++---- .../practice-exercises/2-even-odd-reporter.js | 8 ++++- Week1/practice-exercises/3-recipe-card.js | 21 +++++++++++-- Week1/practice-exercises/4-reading-list.js | 30 ++++++++++++++++++- .../practice-exercises/5-who-wants-a-drink.js | 26 ++++++++++++++-- .../1-traffic-light/traffic-light-1.js | 11 ++++++- .../1-traffic-light/traffic-light-2.js | 11 ++++++- 7 files changed, 107 insertions(+), 15 deletions(-) diff --git a/Week1/practice-exercises/1-remove-the-comma.js b/Week1/practice-exercises/1-remove-the-comma.js index b71cffd..ce88547 100644 --- a/Week1/practice-exercises/1-remove-the-comma.js +++ b/Week1/practice-exercises/1-remove-the-comma.js @@ -1,14 +1,17 @@ /** * We want to remove the comma's in the given string (myString), replace them with a space and log it to the console. - * - * The end result should be: + * + * The end result should be: * hello this is a difficult to read sentence */ -let myString = 'hello,this,is,a,difficult,to,read,sentence'; - - +let myString = "hello,this,is,a,difficult,to,read,sentence"; +myString = myString.split(",").join(" "); +// myString = myString.replace(/,/g, " "); // Alternative solution /* --- Code that will test your solution, do NOT change. Write above this line --- */ -console.assert(myString === 'hello this is a difficult to read sentence', 'There is something wrong with your solution'); \ No newline at end of file +console.assert( + myString === "hello this is a difficult to read sentence", + "There is something wrong with your solution" +); diff --git a/Week1/practice-exercises/2-even-odd-reporter.js b/Week1/practice-exercises/2-even-odd-reporter.js index 6edf23e..adb95d3 100644 --- a/Week1/practice-exercises/2-even-odd-reporter.js +++ b/Week1/practice-exercises/2-even-odd-reporter.js @@ -6,4 +6,10 @@ * If it's odd, log to the console The number [PUT_NUMBER_HERE] is odd!. * If it's even, log to the console The number [PUT_NUMBER_HERE] is even!. */ - +for (let i = 0; i <= 20; i++) { + if (i % 2 === 0) { + console.log(`The number ${i} is even!`); + } else { + console.log(`The number ${i} is odd!`); + } +} diff --git a/Week1/practice-exercises/3-recipe-card.js b/Week1/practice-exercises/3-recipe-card.js index 24bcb54..b5187c9 100644 --- a/Week1/practice-exercises/3-recipe-card.js +++ b/Week1/practice-exercises/3-recipe-card.js @@ -1,14 +1,29 @@ /** * Ever wondered how to make a certain meal? Let's create a recipe list with JavaScript! - * + * * Declare a variable that holds an empty object literal (your meal recipe). * Give the object 3 properties: a title (string), a servings (number) and an ingredients (array of strings) property. * Log each property out separately, using a loop (for, while or do/while) - * + * * Expected result: - * + * * Meal name: Omelette * Serves: 2 * Ingredients: 4 eggs, 2 strips of bacon, 1 tsp salt/pepper */ +const recipe = { + title: "Omelette", + servings: 2, + ingredients: ["4 eggs", "2 strips of bacon", "1 tsp salt/pepper"], +}; + +for (const property in recipe) { + if (property === "title") { + console.log(`Meal name: ${recipe[property]}`); + } else if (property === "servings") { + console.log(`Serves: ${recipe[property]}`); + } else if (property === "ingredients") { + console.log(`Ingredients: ${recipe[property].join(", ")}`); + } +} diff --git a/Week1/practice-exercises/4-reading-list.js b/Week1/practice-exercises/4-reading-list.js index f535657..723d4e3 100644 --- a/Week1/practice-exercises/4-reading-list.js +++ b/Week1/practice-exercises/4-reading-list.js @@ -1,6 +1,6 @@ /** * Keep track of which books you read and which books you want to read! - * + * * Follow the steps: * Declare a variable that holds an array of 3 objects, where each object describes a book and has properties for the title (string), author (string), and alreadyRead (boolean indicating if you read it yet). * Loop through the array of books. @@ -9,3 +9,31 @@ * If you haven't read it log a string like You still need to read "The Lord of the Rings" */ +const books = [ + { + title: "The Hobbit", + author: "J.R.R. Tolkien", + alreadyRead: true, + }, + { + title: "The Lord of the Rings", + author: "J.R.R. Tolkien", + alreadyRead: false, + }, + { + title: "Harry Potter and the Philosopher's Stone", + author: "J.K. Rowling", + alreadyRead: true, + }, +]; + +for (let i = 0; i < books.length; i++) { + const book = books[i]; + console.log(`${book.title} by ${book.author}`); + + if (book.alreadyRead) { + console.log(`You already read "${book.title}"`); + } else { + console.log(`You still need to read "${book.title}"`); + } +} diff --git a/Week1/practice-exercises/5-who-wants-a-drink.js b/Week1/practice-exercises/5-who-wants-a-drink.js index f37f02b..dbc1259 100644 --- a/Week1/practice-exercises/5-who-wants-a-drink.js +++ b/Week1/practice-exercises/5-who-wants-a-drink.js @@ -3,9 +3,31 @@ * * Declare a variable that holds an empty array, called drinkTray. * Create a loop that runs 5 times. On each iteration, push a drink into the drinkTray variable. The drinkTray can only hold at most two instances of the same drink type, for example it can only hold 2 colas, 2 lemonades, 2 waters. - * + * * Log to the console: "Hey guys, I brought a [INSERT VALUES FROM ARRAY]!" (For example: "Hey guys, I brought a cola, cola, lemonade, lemonade, water!") */ // There are 3 different types of drinks: -const drinkTypes = ['cola', 'lemonade', 'water']; \ No newline at end of file +const drinkTypes = ["cola", "lemonade", "water"]; + +const drinkTray = []; +for (let i = 0; i < 5; i++) { + // Get a random drink type + const drinkType = drinkTypes[Math.floor(Math.random() * drinkTypes.length)]; + + // Count the number of instances of the current drink type in the tray + const count = drinkTray.filter((drink) => drink === drinkType).length; + + // Only add the drink if there are less than 2 instances already + if (count < 2) { + drinkTray.push(drinkType); + } else { + // If there are already 2 instances, get a different drink type + const otherDrinkTypes = drinkTypes.filter((type) => type !== drinkType); + const newDrinkType = + otherDrinkTypes[Math.floor(Math.random() * otherDrinkTypes.length)]; + drinkTray.push(newDrinkType); + } +} +// Log the drinks to the console +console.log(`Hey guys, I brought a ${drinkTray.join(", ")}!`); 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..8f71995 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,16 @@ let rotations = 0; while (rotations < 2) { const currentState = trafficLight.state; console.log("The traffic light is on", currentState); - + if (currentState === "green") { + trafficLight.state = "orange"; + } + if (currentState === "orange") { + trafficLight.state = "red"; + } + 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..56567e3 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -13,7 +13,16 @@ 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; // orange + } + if (currentState === "orange") { + trafficLight.stateIndex = 2; // red + } + if (currentState === "red") { + trafficLight.stateIndex = 0; // green + cycle++; + } // TODO // if the color is green, turn it orange // if the color is orange, turn it red From eeb5127634bbe875796790c45302a8c4bb74feb0 Mon Sep 17 00:00:00 2001 From: Vladislav Nachikov Date: Fri, 16 May 2025 19:05:46 +0200 Subject: [PATCH 2/4] Prep exersises from 2d week of JS --- .../1-traffic-light/traffic-light.js | 9 +++- Week2/prep-exercises/2-experiments/index.js | 52 ++++++++----------- 2 files changed, 31 insertions(+), 30 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..e3dcf82 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -9,6 +9,7 @@ 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) { @@ -17,8 +18,14 @@ function getNextStateIndex(trafficLight) { // - 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 1; + } else if (getCurrentState(trafficLight) === "orange") { + return 2; + } else if (getCurrentState(trafficLight) === "red") { + return 0; + } } - // 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 diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..12e894c 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -2,28 +2,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 < sampleSize; i++) { + const x = Math.floor(Math.random() * 6); + valueCounts[x]++; + } const results = []; - // TODO - // Write a for..of loop for the `valueCounts` array created in the previous - // loop. In each loop iteration: - // 1. For each possible value of the die (1-6), compute the percentage of how - // many times that value was thrown. Remember that the first value of - // `valueCounts` represent the die value of 1, etc. - // 2. Convert the computed percentage to a number string with a precision of - // two decimals, e.g. '14.60'. - // 3. Then push that string onto the `results` array. + for (const count of valueCounts) { + const percent = (count / sampleSize) * 100; + results.push(percent.toFixed(2)); + } return results; } @@ -31,16 +20,21 @@ function runExperiment(sampleSize) { function main() { const sampleSizes = [100, 1000, 1000000]; - // TODO - // Write a for..of loop that calls the `runExperiment()` function for each - // value of the `sampleSizes` array. - // Log the results of each experiment as well as the experiment size to the - // console. - // The expected output could look like this: - // - // [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100 - // [ '14.60', '17.10', '19.30', '15.50', '16.70', '16.80' ] 1000 - // [ '16.71', '16.68', '16.69', '16.66', '16.67', '16.59' ] 1000000 + for (const size of sampleSizes) { + const experimentResults = runExperiment(size); + console.log(experimentResults, size); + } } +// TODO +// Write a for..of loop that calls the `runExperiment()` function for each +// value of the `sampleSizes` array. +// Log the results of each experiment as well as the experiment size to the +// console. +// The expected output could look like this: +// +// [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100 +// [ '14.60', '17.10', '19.30', '15.50', '16.70', '16.80' ] 1000 +// [ '16.71', '16.68', '16.69', '16.66', '16.67', '16.59' ] 1000000 + main(); From 4f263fbc12e4c9de4405a4ae0616712795f48aa6 Mon Sep 17 00:00:00 2001 From: Vladislav Nachikov Date: Sat, 24 May 2025 18:25:19 +0200 Subject: [PATCH 3/4] Finished the week3 prep --- .../1-hyf-program/1-find-mentors.js | 19 +++++--- .../1-hyf-program/2-class-list.js | 45 ++++++++++++++++--- 2 files changed, 52 insertions(+), 12 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..2ee43d5 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,15 @@ 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) + ); + return mentorsWhoCanTeach.map((x) => x.name); }; -// You can uncomment out this line to try your function -// console.log(possibleMentorsForModule('using-apis')); + +possibleMentorsForModule(); + +console.log(possibleMentorsForModule("using-apis")); /** * Tjebbe wants to make it even easier for himself. @@ -20,7 +25,9 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const random = possibleMentorsForModule(moduleName); + const randomIndex = Math.floor(Math.random()) * random.length; + return random[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..f6a42f6 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,35 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function + const StudentName = students.filter((student) => + student.class.includes(className) + ); + + const ListOfStudents = StudentName.map((student) => students.name); + + const currentClass = classes.find((cls) => cls.name === className); + const currentModule = currentClass.currentModule; + + const mentorNames = mentors + .filter((mentor) => mentor.nowTeaching === currentModule) + .map((mentor) => mentor.name); + + const studentList = StudentName.map((student) => ({ + name: student.name, + role: "student", + })); + + const mentorList = mentors + .filter((mentor) => mentor.nowTeaching === currentModule) + .map((mentor) => ({ + name: mentor.name, + role: "mentor", + })); + + return [...studentList, ...mentorList]; }; -// 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 +55,15 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - // TODO complete this function + const activeClasses = classes.filter((cls) => cls.active); + + const result = {}; + + activeClasses.forEach((cls) => { + result[cls.name] = getPeopleOfClass(cls.name); + }); + + return result; }; -// You can uncomment out this line to try your function -// console.log(getActiveClasses()); + +console.log(getActiveClasses()); From fe37a6a978e7bdf578bb45a82bf906c9ba161783 Mon Sep 17 00:00:00 2001 From: Vladislav Nachikov Date: Sat, 31 May 2025 18:20:41 +0200 Subject: [PATCH 4/4] Vlad-Nachikov-w4-prep --- Week4/prep-exercises/1-wallet/ex2-classes.js | 44 ++++++++++++++++--- Week4/prep-exercises/1-wallet/ex3-object.js | 38 +++++++++++++--- .../1-wallet/ex4-object-shared-methods.js | 44 +++++++++++++++---- .../prep-exercises/1-wallet/ex5-prototype.js | 12 ++--- Week4/prep-exercises/2-game-of-life/Cell.js | 23 +++++++--- 5 files changed, 127 insertions(+), 34 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..a9b4ad8 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -1,12 +1,16 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from "./euroFormatter.js"; class Wallet { #name; #cash; + #dailyAllowance; + #dayTotalWithdrawals; - constructor(name, cash) { + constructor(name, cash = 0) { this.#name = name; this.#cash = cash; + this.#dailyAllowance = 40; + this.#dayTotalWithdrawals = 0; } get name() { @@ -18,12 +22,18 @@ class Wallet { } withdraw(amount) { - if (this.#cash - amount < 0) { + if (this.#cash < 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; } @@ -33,8 +43,22 @@ class Wallet { wallet.name }` ); + const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); + if (withdrawnAmount > 0) { + wallet.deposit(withdrawnAmount); + } + } + + setDailyAllowance(newAllowance) { + this.#dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + } + + resetDailyAllowance() { + this.#dayTotalWithdrawals = 0; } reportBalance() { @@ -45,9 +69,9 @@ 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); walletJane.transferInto(walletJoe, 25); @@ -58,6 +82,12 @@ function main() { walletJack.reportBalance(); walletJoe.reportBalance(); walletJane.reportBalance(); + + walletJane.setDailyAllowance(100); + walletJane.resetDailyAllowance(); + walletJane.transferInto(walletJoe, 50); + + 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..ba77b0f 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,21 +1,29 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from "./euroFormatter.js"; function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit: function (amount) { this._cash += amount; }, withdraw: function (amount) { + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Daily allowance exceeded!`); + return 0; + } + if (this._cash - amount < 0) { console.log(`Insufficient funds!`); return 0; } this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; }, @@ -26,7 +34,9 @@ function createWallet(name, cash = 0) { } to ${wallet.getName()}` ); const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); + if (withdrawnAmount > 0) { + wallet.deposit(withdrawnAmount); + } }, reportBalance: function () { @@ -38,23 +48,37 @@ function createWallet(name, cash = 0) { getName: function () { return this._name; }, + + resetDailyAllowance: function () { + this._dayTotalWithdrawals = 0; + }, + + setDailyAllowance: function (newAllowance) { + this._dailyAllowance = newAllowance; + }, }; } 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); + walletJane.transferInto(walletJoe, 25); // Exceeds limit (default = 40) walletJane.deposit(20); - walletJane.transferInto(walletJoe, 25); + walletJane.transferInto(walletJoe, 25); // OK if still within daily limit walletJack.reportBalance(); walletJoe.reportBalance(); walletJane.reportBalance(); + + walletJane.setDailyAllowance(100); + walletJane.resetDailyAllowance(); + walletJane.transferInto(walletJoe, 50); // Now works after reset + increased limit + + 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..ba2479a 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -1,16 +1,22 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from "./euroFormatter.js"; function deposit(amount) { this._cash += amount; } function withdraw(amount) { + if (this._dayTotalWithdrawals + amount > this._dailyAllowance) { + console.log(`Daily allowance exceeded!`); + return 0; + } + if (this._cash - amount < 0) { console.log(`Insufficient funds!`); return 0; } this._cash -= amount; + this._dayTotalWithdrawals += amount; return amount; } @@ -20,8 +26,11 @@ function transferInto(wallet, amount) { this._name } to ${wallet.getName()}` ); + const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); + if (withdrawnAmount > 0) { + wallet.deposit(withdrawnAmount); + } } function reportBalance() { @@ -34,32 +43,51 @@ function getName() { return this._name; } +function resetDailyAllowance() { + this._dayTotalWithdrawals = 0; +} + +function setDailyAllowance(newAllowance) { + this._dailyAllowance = newAllowance; +} + function createWallet(name, cash = 0) { return { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit, withdraw, transferInto, reportBalance, getName, + resetDailyAllowance, + setDailyAllowance, }; } 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); // OK + walletJane.transferInto(walletJoe, 25); // Exceeds allowance walletJane.deposit(20); - walletJane.transferInto(walletJoe, 25); + walletJane.transferInto(walletJoe, 25); // OK walletJack.reportBalance(); walletJoe.reportBalance(); walletJane.reportBalance(); + + // Пример изменения лимита и сброса + walletJane.setDailyAllowance(100); + walletJane.resetDailyAllowance(); + walletJane.transferInto(walletJoe, 50); // Теперь OK + + 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..439ded6 100644 --- a/Week4/prep-exercises/1-wallet/ex5-prototype.js +++ b/Week4/prep-exercises/1-wallet/ex5-prototype.js @@ -1,4 +1,4 @@ -import eurosFormatter from './euroFormatter.js'; +import eurosFormatter from "./euroFormatter.js"; function Wallet(name, cash) { this._name = name; @@ -26,7 +26,9 @@ Wallet.prototype.transferInto = function (wallet, amount) { } to ${wallet.getName()}` ); const withdrawnAmount = this.withdraw(amount); - wallet.deposit(withdrawnAmount); + if (withdrawnAmount > 0) { + wallet.deposit(withdrawnAmount); + } }; Wallet.prototype.reportBalance = function () { @@ -40,9 +42,9 @@ 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); walletJane.transferInto(walletJoe, 25); diff --git a/Week4/prep-exercises/2-game-of-life/Cell.js b/Week4/prep-exercises/2-game-of-life/Cell.js index cac08da..ea7c5aa 100644 --- a/Week4/prep-exercises/2-game-of-life/Cell.js +++ b/Week4/prep-exercises/2-game-of-life/Cell.js @@ -14,11 +14,11 @@ export default class Cell { this.y = y; this.alive = Math.random() > 0.5; this.nextAlive = false; + this.lifeTime = this.alive ? 1 : 0; } draw(context) { - // Draw this background - context.fillStyle = '#303030'; + context.fillStyle = "#303030"; context.fillRect( this.x * Cell.size, this.y * Cell.size, @@ -27,8 +27,12 @@ export default class Cell { ); if (this.alive) { - // Draw living this inside background - context.fillStyle = `rgb(24, 215, 236)`; + let opacity = 0.25; + if (this.lifeTime === 2) opacity = 0.5; + else if (this.lifeTime === 3) opacity = 0.75; + else if (this.lifeTime >= 4) opacity = 1; + + context.fillStyle = `rgba(24, 215, 236, ${opacity})`; context.fillRect( this.x * Cell.size + 1, this.y * Cell.size + 1, @@ -40,15 +44,20 @@ export default class Cell { liveAndLetDie(aliveNeighbors) { if (aliveNeighbors === 2) { - // Living cell remains living, dead cell remains dead this.nextAlive = this.alive; } else if (aliveNeighbors === 3) { - // Dead cell becomes living, living cell remains living this.nextAlive = true; } else { - // Living cell dies, dead cell remains dead this.nextAlive = false; } + + if (this.alive && this.nextAlive) { + this.lifeTime += 1; + } else if (!this.alive && this.nextAlive) { + this.lifeTime = 1; + } else if (this.alive && !this.nextAlive) { + this.lifeTime = 0; + } } update() {