From b55215da22903f639291be9ca92e6f9d1e960bb0 Mon Sep 17 00:00:00 2001 From: mo-muchunu Date: Tue, 22 Jul 2025 15:58:21 +0100 Subject: [PATCH] Completed object and array destructuring exercises using filtering, looping, and reduce. --- Sprint-1/destructuring/exercise-1/exercise.js | 2 +- Sprint-1/destructuring/exercise-2/exercise.js | 16 ++++++++++++++++ Sprint-1/destructuring/exercise-3/exercise.js | 13 +++++++++++++ 3 files changed, 30 insertions(+), 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}.` ); diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..866fb647 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,19 @@ let hogwarts = [ occupation: "Teacher", }, ]; + + +// Task 1 ---> List all Gryffindor house members +hogwarts + .filter(({ house }) => house === "Gryffindor") // Filter the array and keep only objects with 'house' matching 'Gryffindor' + .forEach(({ firstName, lastName }) => { // Loop through each filtered object in filtered array, and extract first and last names + console.log(`${firstName} ${lastName}`); + }); + + +// Task 2 ---> names of teachers who have pets +for (const { firstName, lastName, occupation, pet } of hogwarts) { // Loop through array, destructuring 4 properties from each object + if (occupation === "Teacher" && pet) { // condition to check person's occupation and pet status + console.log(`${firstName} ${lastName}`); + } +} diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..29b61275 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,16 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +// Print receipt for takeout order +console.log("QTY ITEM TOTAL"); // Print receipt header row with even spacing for 3 columns + +const orderTotal = order.reduce((sum, { itemName, quantity, unitPricePence }) => { // Using reduce() to process each item in 'order' array and sum() to tally total order cost + const itemPriceTotal = (unitPricePence * quantity) / 100; // Destructure for required unit price and quantity values from each object: calculate in then divide by 100 for total in pounds + console.log( + `${quantity.toString().padEnd(8)}${itemName.padEnd(20)}${itemPriceTotal.toFixed(2)}` // Add spacing to align row output neatly and format total prices to 2 decimal places + ); + return sum + itemPriceTotal; // Add item's total price to the running sum +}, 0); + +console.log(`\nTotal: ${orderTotal.toFixed(2)}`); // Print final total cost, formatted to 2 decimal places- on a new line