Skip to content
78 changes: 75 additions & 3 deletions Sprint-1/destructuring/exercise-1/exercise.js
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions Sprint-1/destructuring/exercise-2/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
});
11 changes: 11 additions & 0 deletions Sprint-1/destructuring/exercise-3/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`);
Loading