From 136462ddbf49ae2282f2f2e5b974d685547f1f3a Mon Sep 17 00:00:00 2001 From: sedazam Date: Sat, 26 Jul 2025 12:42:10 +0100 Subject: [PATCH 1/7] Fix function parameter to enable destructuring in introduceYourself --- Sprint-1/destructuring/exercise-1/exercise.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..90eb4795 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -6,7 +6,7 @@ const personOne = { // Update the parameter to this function to make it work. // Don't change anything else. -function introduceYourself(___________________________) { +function introduceYourself({name, age, favouriteFood}) { console.log( `Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.` ); From 49b6859e1cdbe9fe5d4c5cd26d407756e4646812 Mon Sep 17 00:00:00 2001 From: sedazam Date: Sun, 27 Jul 2025 19:55:44 +0100 Subject: [PATCH 2/7] Refactor hogwarts array iteration to use destructuring for clarity --- Sprint-1/destructuring/exercise-2/exercise.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..3894144a 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,14 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +hogwarts.forEach(({ firstName, lastName, house }) => { + if (house === "Gryffindor") { + console.log(`${firstName} ${lastName}`); + } +}); +hogwarts.forEach(({ firstName, lastName, pet, occupation }) => { + if (occupation === "Teacher" && pet !== null) { + console.log(`${firstName} ${lastName}`); + } +}); \ No newline at end of file From 805e5fefc4115affaa303db54ee90cfd1e92015a Mon Sep 17 00:00:00 2001 From: sedazam Date: Sun, 27 Jul 2025 19:57:59 +0100 Subject: [PATCH 3/7] Enhance hogwarts array iteration to include pet and occupation in destructuring --- Sprint-1/destructuring/exercise-2/exercise.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index 3894144a..7a043fa3 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -71,7 +71,7 @@ let hogwarts = [ }, ]; -hogwarts.forEach(({ firstName, lastName, house }) => { +hogwarts.forEach(({ firstName, lastName, house, pet, occupation }) => { if (house === "Gryffindor") { console.log(`${firstName} ${lastName}`); } From 851f70adee04ed84803c5858176ff4a0601bf0f5 Mon Sep 17 00:00:00 2001 From: sedazam Date: Sun, 27 Jul 2025 20:03:58 +0100 Subject: [PATCH 4/7] Refactor hogwarts array iteration to remove redundant loop for Gryffindor --- Sprint-1/destructuring/exercise-2/exercise.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index 7a043fa3..aedec7fc 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -75,8 +75,6 @@ hogwarts.forEach(({ firstName, lastName, house, pet, occupation }) => { if (house === "Gryffindor") { console.log(`${firstName} ${lastName}`); } -}); -hogwarts.forEach(({ firstName, lastName, pet, occupation }) => { if (occupation === "Teacher" && pet !== null) { console.log(`${firstName} ${lastName}`); } From 78abc5b8581ce85e0f192d6a58605cccbf3ade07 Mon Sep 17 00:00:00 2001 From: sedazam Date: Sun, 27 Jul 2025 20:39:44 +0100 Subject: [PATCH 5/7] Add order processing to calculate and display total cost --- Sprint-1/destructuring/exercise-3/exercise.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..3485a7cd 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,10 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +console.log("QTY ITEM TOTAL"); +order.forEach(({ itemName, quantity, unitPricePence }) => { + let itemTotal = (quantity * unitPricePence) / 100; + totalCost += itemTotal; + console.log(`${String(quantity).padEnd(8)} ${itemName.padEnd(20)} ${itemTotal.toFixed(2)}`); +}); From 87d0f10de8193ff223759db80ecb0623126e0264 Mon Sep 17 00:00:00 2001 From: sedazam Date: Sun, 27 Jul 2025 20:52:51 +0100 Subject: [PATCH 6/7] Refactor order total calculation for improved readability --- Sprint-1/destructuring/exercise-3/exercise.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index 3485a7cd..067d5b49 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -8,8 +8,12 @@ let order = [ ]; console.log("QTY ITEM TOTAL"); +let totalCost = 0; + order.forEach(({ itemName, quantity, unitPricePence }) => { let itemTotal = (quantity * unitPricePence) / 100; totalCost += itemTotal; - console.log(`${String(quantity).padEnd(8)} ${itemName.padEnd(20)} ${itemTotal.toFixed(2)}`); + console.log(`${String(quantity).padEnd(8)}${itemName.padEnd(20)}${itemTotal.toFixed(2)}`); }); + +console.log(`\nTotal: ${totalCost.toFixed(2)}`); \ No newline at end of file From cda4d4c73f9fe852f4a86953c771d1d23dc1efa7 Mon Sep 17 00:00:00 2001 From: sedazam Date: Fri, 1 Aug 2025 23:39:34 +0100 Subject: [PATCH 7/7] Refactor destructuring examples and enhance error handling for clarity --- Sprint-1/destructuring/exercise-1/exercise.js | 76 ++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 90eb4795..37355403 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -1,11 +1,11 @@ +// Original exercise - demonstrates basic object destructuring const personOne = { name: "Popeye", age: 34, favouriteFood: "Spinach", }; -// Update the parameter to this function to make it work. -// Don't change anything else. +// Basic destructuring in function parameter function introduceYourself({name, age, favouriteFood}) { console.log( `Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.` @@ -13,3 +13,75 @@ function introduceYourself({name, age, favouriteFood}) { } introduceYourself(personOne); + +// Additional examples to demonstrate different destructuring patterns +const personTwo = { + firstName: "Bruce", + lastName: "Wayne", + occupation: "Superhero", + city: "Gotham", + age: 35 +}; + +// Destructuring with default values +function introduceWithDefaults({firstName, lastName, occupation = "Unknown", age = "Unknown"}) { + console.log( + `Hello, I'm ${firstName} ${lastName}. I work as a ${occupation} and I'm ${age} years old.` + ); +} + +// Destructuring with renaming +function introduceWithAlias({firstName: first, lastName: last, city}) { + console.log( + `Hi, I'm ${first} ${last} from ${city}.` + ); +} + +// Nested destructuring example +const personThree = { + name: "Clark", + details: { + age: 30, + powers: ["Flight", "Super Strength", "Heat Vision"] + }, + location: { + city: "Metropolis", + planet: "Earth" + } +}; + +function introduceWithNested({name, details: {age, powers}, location: {city}}) { + console.log( + `${name} is ${age} years old, has powers like ${powers.join(", ")}, and lives in ${city}.` + ); +} + +// Error handling with destructuring +function introduceSafely(person) { + try { + const {name, age, favouriteFood} = person; + console.log( + `Hello, my name is ${name || "Unknown"}. I am ${age || "Unknown"} years old and my favourite food is ${favouriteFood || "Unknown"}.` + ); + } catch (error) { + console.log("Sorry, I couldn't get the person's information."); + } +} + +// Test the different functions +console.log("\n--- Basic Destructuring ---"); +introduceYourself(personOne); + +console.log("\n--- Destructuring with Defaults ---"); +introduceWithDefaults(personTwo); +introduceWithDefaults({firstName: "John", lastName: "Doe"}); // Missing properties use defaults + +console.log("\n--- Destructuring with Aliases ---"); +introduceWithAlias(personTwo); + +console.log("\n--- Nested Destructuring ---"); +introduceWithNested(personThree); + +console.log("\n--- Safe Destructuring ---"); +introduceSafely(personOne); +introduceSafely(null); // Handles invalid input gracefully