diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..37355403 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -1,15 +1,87 @@ +// 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. -function introduceYourself(___________________________) { +// 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}.` ); } 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 diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..aedec7fc 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,12 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +hogwarts.forEach(({ firstName, lastName, house, pet, occupation }) => { + if (house === "Gryffindor") { + console.log(`${firstName} ${lastName}`); + } + if (occupation === "Teacher" && pet !== null) { + console.log(`${firstName} ${lastName}`); + } +}); \ No newline at end of file diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..067d5b49 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,14 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +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(`\nTotal: ${totalCost.toFixed(2)}`); \ No newline at end of file