From 213400add681382bbb222783b063060b84685fa8 Mon Sep 17 00:00:00 2001 From: Majd Hamde Date: Sun, 17 Aug 2025 13:00:48 +0200 Subject: [PATCH 01/11] solutin --- .../1-traffic-light/traffic-light-1.js | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 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..abf0433 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -12,20 +12,13 @@ while (rotations < 2) { const currentState = trafficLight.state; console.log("The traffic light is on", currentState); - // 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 + // logic to change the state + if (currentState === "green") { + trafficLight.state = "orange"; + } else if (currentState === "orange") { + trafficLight.state = "red"; + } else if (currentState === "red") { + rotations += 1; // completed one full cycle + trafficLight.state = "green"; + } } - -/** - * The output should be: - -The traffic light is on green -The traffic light is on orange -The traffic light is on red -The traffic light is on green -The traffic light is on orange -The traffic light is on red - -*/ From adb818f62cd8b0ae233af16c9cbd970195ac65ea Mon Sep 17 00:00:00 2001 From: Majd Hamde Date: Sun, 17 Aug 2025 13:07:31 +0200 Subject: [PATCH 02/11] solution 2 --- .../1-traffic-light/traffic-light-2.js | 28 +++++-------------- 1 file changed, 7 insertions(+), 21 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 8c6ba95..899972e 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -1,9 +1,5 @@ "use strict"; -/** - * The `possibleStates` property define the states (in this case: colours) - * in which the traffic light can be. - * The `stateIndex` property indicates which of the possible states is current. - */ + const trafficLight = { possibleStates: ["green", "orange", "red"], stateIndex: 0, @@ -14,20 +10,10 @@ while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); - // 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 + if (trafficLight.stateIndex === trafficLight.possibleStates.length - 1) { + cycle += 1; + trafficLight.stateIndex = 0; + } else { + trafficLight.stateIndex += 1; + } } - -/** - * The output should be: - -The traffic light is on green -The traffic light is on orange -The traffic light is on red -The traffic light is on green -The traffic light is on orange -The traffic light is on red - -*/ From cf9b896775eb1252b6139d22b26a33746ac6e460 Mon Sep 17 00:00:00 2001 From: majdjadalhaq Date: Fri, 22 Aug 2025 13:31:42 +0200 Subject: [PATCH 03/11] Update traffic-light.js --- Week2/prep-exercises/1-traffic-light/traffic-light.js | 2 ++ 1 file changed, 2 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..b8817d8 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,6 +18,7 @@ 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 + return (trafficLight.stateIndex + 1) % trafficLight.possibleStates.length; } // This function loops for the number of seconds specified by the `secs` From ec18d58d6965c3fe8a4a7cd87abac2786aa7d4ba Mon Sep 17 00:00:00 2001 From: majdjadalhaq Date: Fri, 22 Aug 2025 13:33:45 +0200 Subject: [PATCH 04/11] Update index.js --- Week2/prep-exercises/2-experiments/index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..84134bf 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -12,6 +12,10 @@ function runExperiment(sampleSize) { // 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 dieRoll = Math.floor(Math.random() * 6) + 1; // 1-6 + valueCounts[dieRoll - 1] += 1; + } const results = []; @@ -24,6 +28,10 @@ function runExperiment(sampleSize) { // 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 percentage = (count / sampleSize) * 100; + results.push(percentage.toFixed(2)); + } return results; } @@ -41,6 +49,10 @@ function main() { // [ '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 experimentResult = runExperiment(size); + console.log(experimentResult, size); + } } main(); From d0c64d992d750dd81aeef0a93c70a7052b8135d3 Mon Sep 17 00:00:00 2001 From: majdjadalhaq Date: Sun, 31 Aug 2025 10:36:20 +0200 Subject: [PATCH 05/11] 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..c37cf52 100644 --- a/Week3/prep-exercises/1-hyf-program/1-find-mentors.js +++ b/Week3/prep-exercises/1-hyf-program/1-find-mentors.js @@ -8,7 +8,9 @@ import { modules, students, mentors, classes } from "./hyf.js"; * ['John', 'Mary'] */ const possibleMentorsForModule = (moduleName) => { - // TODO complete this function + return mentors + .filter((mentor) => mentor.canTeach.includes(moduleName)) + .map((mentor) => mentor.name); }; // You can uncomment out this line to try your function // console.log(possibleMentorsForModule('using-apis')); @@ -20,7 +22,12 @@ const possibleMentorsForModule = (moduleName) => { * It should return a single name. */ const findMentorForModule = (moduleName) => { - // TODO complete this function + const possibleMentors = possibleMentorsForModule(moduleName); + if (possibleMentors.length === 0) { + return null; // No mentor available for this module + } + const randomIndex = Math.floor(Math.random() * possibleMentors.length); + return possibleMentors[randomIndex]; }; // You can uncomment out this line to try your function // console.log(findMentorForModule('javascript')); From 634f49f35a2b93927ef9b4106b5de82fc8570f69 Mon Sep 17 00:00:00 2001 From: majdjadalhaq Date: Sun, 31 Aug 2025 10:38:32 +0200 Subject: [PATCH 06/11] Update 2-class-list.js --- .../1-hyf-program/2-class-list.js | 43 +++++++++++++++++-- 1 file changed, 39 insertions(+), 4 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..0e10257 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,38 @@ import { modules, students, mentors, classes } from "./hyf.js"; * [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }] */ const getPeopleOfClass = (className) => { - // TODO complete this function + const existingClass = classes.find((c) => c.name === className); + if (!existingClass) { + return; + } + + // find the current module for this class (only if active) + const currentModule = existingClass.active && existingClass.currentModule; + + // get all students of this class + const currentStudents = students + .filter((student) => student.class === className) + .map((student) => { + return { + name: student.name, + role: "student", + }; + }); + + // get mentors teaching this module + const currentMentors = mentors + .filter((mentor) => mentor.nowTeaching.includes(currentModule)) + .map((mentor) => { + return { + name: mentor.name, + role: "mentor", + }; + }); + + return [...currentStudents, ...currentMentors]; }; // 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 +58,14 @@ const getPeopleOfClass = (className) => { * } */ const getActiveClasses = () => { - // TODO complete this function + const activeClasses = classes.filter((c) => c.active === true); + const result = {}; + + activeClasses.forEach((activeClass) => { + result[activeClass.name] = getPeopleOfClass(activeClass.name); + }); + + return result; }; // You can uncomment out this line to try your function -// console.log(getActiveClasses()); +console.log(getActiveClasses()); From 76c68736f18cfacb11f78b65cc2b2ae25ba2002f Mon Sep 17 00:00:00 2001 From: majdjadalhaq Date: Fri, 5 Sep 2025 12:32:11 +0200 Subject: [PATCH 07/11] Update ex2-classes.js --- Week4/prep-exercises/1-wallet/ex2-classes.js | 42 ++++++++++++++++---- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex2-classes.js b/Week4/prep-exercises/1-wallet/ex2-classes.js index f016137..97ca019 100644 --- a/Week4/prep-exercises/1-wallet/ex2-classes.js +++ b/Week4/prep-exercises/1-wallet/ex2-classes.js @@ -3,40 +3,64 @@ import eurosFormatter from './euroFormatter.js'; class Wallet { #name; #cash; + #dailyAllowance = 40; // Maximum daily withdrawal + #dayTotalWithdrawals = 0; // Total withdrawn today - constructor(name, cash) { + constructor(name, cash = 0) { this.#name = name; this.#cash = cash; } + // Getter for the name get name() { return this.#name; } + // Deposit money deposit(amount) { this.#cash += amount; } + // Withdraw money respecting cash and daily allowance 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; + this.#dayTotalWithdrawals += amount; return amount; } + // Transfer money to another wallet transferInto(wallet, amount) { console.log( - `Transferring ${eurosFormatter.format(amount)} from ${this.name} to ${ - wallet.name - }` + `Transferring ${eurosFormatter.format(amount)} from ${this.name} to ${wallet.name}` ); const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); } + // Set a new daily allowance + setDailyAllowance(newAllowance) { + this.#dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + } + + // Reset daily withdrawals + resetDailyAllowance() { + this.#dayTotalWithdrawals = 0; + } +//headache + // Report balance reportBalance() { console.log( `Name: ${this.name}, balance: ${eurosFormatter.format(this.#cash)}` @@ -49,12 +73,14 @@ function main() { const walletJoe = new Wallet('Joe', 10); const walletJane = new Wallet('Jane', 20); - walletJack.transferInto(walletJoe, 50); - walletJane.transferInto(walletJoe, 25); + walletJack.transferInto(walletJoe, 50); // Jack to Joe + walletJack.setDailyAllowance(80); // Update Jack's daily allowance + walletJack.transferInto(walletJoe, 50); // Jack to Joe again + walletJane.transferInto(walletJoe, 25); // Jane to Joe (should fail if over allowance) walletJane.deposit(20); - walletJane.transferInto(walletJoe, 25); - + walletJane.transferInto(walletJoe, 25); // Jane to Joe +// I HATE ; walletJack.reportBalance(); walletJoe.reportBalance(); walletJane.reportBalance(); From 263968c18cb6ab37d58b408c312286f944d9e34b Mon Sep 17 00:00:00 2001 From: majdjadalhaq Date: Fri, 5 Sep 2025 12:51:50 +0200 Subject: [PATCH 08/11] Update ex3-object.js --- Week4/prep-exercises/1-wallet/ex3-object.js | 29 ++++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex3-object.js b/Week4/prep-exercises/1-wallet/ex3-object.js index e94faac..ebfae15 100644 --- a/Week4/prep-exercises/1-wallet/ex3-object.js +++ b/Week4/prep-exercises/1-wallet/ex3-object.js @@ -1,6 +1,10 @@ import eurosFormatter from './euroFormatter.js'; function createWallet(name, cash = 0) { + // Private(not that much ) properties using naming convention + let _dailyAllowance = 40; + let _dayTotalWithdrawals = 0; + return { _name: name, _cash: cash, @@ -15,15 +19,19 @@ function createWallet(name, cash = 0) { return 0; } + if (_dayTotalWithdrawals + amount > _dailyAllowance) { + console.log(`Insufficient remaining daily allowance!`); + return 0; + } +// forgot what im doing mid way and just stare at the screen this._cash -= amount; + _dayTotalWithdrawals += amount; return amount; }, transferInto: function (wallet, amount) { console.log( - `Transferring ${eurosFormatter.format(amount)} from ${ - this._name - } to ${wallet.getName()}` + `Transferring ${eurosFormatter.format(amount)} from ${this._name} to ${wallet.getName()}` ); const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); @@ -38,6 +46,17 @@ function createWallet(name, cash = 0) { getName: function () { return this._name; }, + + setDailyAllowance: function (newAllowance) { + _dailyAllowance = newAllowance; + console.log( + `Daily allowance set to: ${eurosFormatter.format(newAllowance)}` + ); + }, + + resetDailyAllowance: function () { + _dayTotalWithdrawals = 0; + }, }; } @@ -47,8 +66,10 @@ function main() { const walletJane = createWallet('Jane', 20); walletJack.transferInto(walletJoe, 50); - walletJane.transferInto(walletJoe, 25); + walletJack.setDailyAllowance(80); + walletJack.transferInto(walletJoe, 50); + walletJane.transferInto(walletJoe, 25); walletJane.deposit(20); walletJane.transferInto(walletJoe, 25); From caf9a5ecc2a663811d9009c08c3a858e5acf2350 Mon Sep 17 00:00:00 2001 From: majdjadalhaq Date: Fri, 5 Sep 2025 13:07:58 +0200 Subject: [PATCH 09/11] Update ex4-object-shared-methods.js --- .../1-wallet/ex4-object-shared-methods.js | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) 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..798db73 100644 --- a/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js +++ b/Week4/prep-exercises/1-wallet/ex4-object-shared-methods.js @@ -1,5 +1,6 @@ import eurosFormatter from './euroFormatter.js'; +// Shared methods function deposit(amount) { this._cash += amount; } @@ -10,15 +11,19 @@ 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; } function transferInto(wallet, amount) { console.log( - `Transferring ${eurosFormatter.format(amount)} from ${ - this._name - } to ${wallet.getName()}` + `Transferring ${eurosFormatter.format(amount)} from ${this._name} to ${wallet.getName()}` ); const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); @@ -34,16 +39,31 @@ function getName() { return this._name; } +function setDailyAllowance(newAllowance) { + this._dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`); +} + +function resetDailyAllowance() { + this._dayTotalWithdrawals = 0; +} + +// Factory function function createWallet(name, cash = 0) { - return { + const wallet = { _name: name, _cash: cash, + _dailyAllowance: 40, + _dayTotalWithdrawals: 0, deposit, withdraw, transferInto, reportBalance, getName, + setDailyAllowance, + resetDailyAllowance, }; + return wallet; } function main() { @@ -52,8 +72,10 @@ function main() { const walletJane = createWallet('Jane', 20); walletJack.transferInto(walletJoe, 50); - walletJane.transferInto(walletJoe, 25); + walletJack.setDailyAllowance(80); + walletJack.transferInto(walletJoe, 50); + walletJane.transferInto(walletJoe, 25); walletJane.deposit(20); walletJane.transferInto(walletJoe, 25); From 9040e09e6c5cc83e9958ff0b61ff4082acc874d4 Mon Sep 17 00:00:00 2001 From: majdjadalhaq Date: Fri, 5 Sep 2025 13:14:34 +0200 Subject: [PATCH 10/11] Update ex5-prototype.js --- .../prep-exercises/1-wallet/ex5-prototype.js | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/Week4/prep-exercises/1-wallet/ex5-prototype.js b/Week4/prep-exercises/1-wallet/ex5-prototype.js index 7cba410..c054204 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; // Maximum daily withdrawal + this._dayTotalWithdrawals = 0; // Total withdrawn today } Wallet.prototype.deposit = function (amount) { @@ -15,15 +17,19 @@ 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; }; Wallet.prototype.transferInto = function (wallet, amount) { console.log( - `Transferring ${eurosFormatter.format(amount)} from ${ - this._name - } to ${wallet.getName()}` + `Transferring ${eurosFormatter.format(amount)} from ${this._name} to ${wallet.getName()}` ); const withdrawnAmount = this.withdraw(amount); wallet.deposit(withdrawnAmount); @@ -39,14 +45,26 @@ Wallet.prototype.getName = function () { return this._name; }; +// New methods for daily allowance +Wallet.prototype.setDailyAllowance = function (newAllowance) { + this._dailyAllowance = newAllowance; + console.log(`Daily allowance set to: ${eurosFormatter.format(newAllowance)}`); +}; + +Wallet.prototype.resetDailyAllowance = function () { + this._dayTotalWithdrawals = 0; +}; + function main() { 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.setDailyAllowance(80); + walletJack.transferInto(walletJoe, 50); + walletJane.transferInto(walletJoe, 25); walletJane.deposit(20); walletJane.transferInto(walletJoe, 25); From 112f5b720d2403641e3d298918ae991063c1a248 Mon Sep 17 00:00:00 2001 From: majdjadalhaq <213489665+majdjadalhaq@users.noreply.github.com> Date: Mon, 8 Sep 2025 04:33:52 -0400 Subject: [PATCH 11/11] Implement lifeTime and opacity for Game of Life cells --- Week4/prep-exercises/2-game-of-life/Cell.js | 29 ++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Week4/prep-exercises/2-game-of-life/Cell.js b/Week4/prep-exercises/2-game-of-life/Cell.js index cac08da..cebce0a 100644 --- a/Week4/prep-exercises/2-game-of-life/Cell.js +++ b/Week4/prep-exercises/2-game-of-life/Cell.js @@ -14,6 +14,7 @@ export default class Cell { this.y = y; this.alive = Math.random() > 0.5; this.nextAlive = false; + this.lifeTime = this.alive ? 1 : 0; } draw(context) { @@ -27,8 +28,20 @@ export default class Cell { ); if (this.alive) { + // Calculate opacity based on lifeTime + let opacity; + if (this.lifeTime === 1) { + opacity = 0.25; + } else if (this.lifeTime === 2) { + opacity = 0.5; + } else if (this.lifeTime === 3) { + opacity = 0.75; + } else { + opacity = 1; // lifeTime >= 4 + } + // Draw living this inside background - context.fillStyle = `rgb(24, 215, 236)`; + context.fillStyle = `rgba(24, 215, 236, ${opacity})`; context.fillRect( this.x * Cell.size + 1, this.y * Cell.size + 1, @@ -52,6 +65,20 @@ export default class Cell { } update() { + const wasAlive = this.alive; this.alive = this.nextAlive; + + // Update lifeTime based on the rules + if (this.alive && wasAlive) { + // Living cell that remains living - increment lifeTime + this.lifeTime++; + } else if (!this.alive && wasAlive) { + // Living cell that dies - reset lifeTime to zero + this.lifeTime = 0; + } else if (this.alive && !wasAlive) { + // Dead cell that is brought to life - reset lifeTime to one + this.lifeTime = 1; + } + // Dead cell that remains dead - lifeTime stays 0 (no change needed) } }