From 9bc1fe61e639660a8798acaa31726f6f3a2f709a Mon Sep 17 00:00:00 2001 From: Mahmoud Shaabo Date: Mon, 13 Apr 2026 11:47:47 +0100 Subject: [PATCH] Complete Object Destructuring exercises --- Sprint-1/destructuring/exercise-1/exercise.js | 5 ++- Sprint-1/destructuring/exercise-2/exercise.js | 23 ++++++++++++++ Sprint-1/destructuring/exercise-3/exercise.js | 31 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..7305603e 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -6,7 +6,10 @@ const personOne = { // Update the parameter to this function to make it work. // Don't change anything else. -function introduceYourself(___________________________) { +function introduceYourself({ name, age, favouriteFood }) { + // The curly braces in the parameter tell JavaScript: + // "When this function receives an object, automatically extract + // name, age, and favouriteFood from it as separate variables." 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..37e3e3c0 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,26 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +// Task 1: Display names of all Gryffindor members. +// The "for...of" loop goes through each object in the array one by one. +// We use { firstName, lastName, house } to destructure only the three +// properties we need from each object — we ignore the rest. +for (const { firstName, lastName, house } of hogwarts) { + if (house === "Gryffindor") { + console.log(`${firstName} ${lastName}`); + } +} + +// Task 2: Display names of teachers who have pets. +// (A blank line separates Task 1 output from Task 2 output in the console.) +// We destructure four properties this time: firstName, lastName, +// occupation, and pet. +// We check two conditions at once using the && (AND) operator: +// 1. occupation must be "Teacher" +// 2. pet must not be null (meaning they actually have a pet) +for (const { firstName, lastName, occupation, pet } of hogwarts) { + if (occupation === "Teacher" && pet !== null) { + console.log(`${firstName} ${lastName}`); + } +} diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..f0cb8665 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,34 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +// padEnd(n) adds spaces after a string until it reaches n characters wide. +// This makes columns line up neatly in the console output. +// Example: "QTY".padEnd(8) gives "QTY " (5 spaces added, total 8 chars). +console.log("QTY".padEnd(8) + "ITEM".padEnd(20) + "TOTAL"); + +// This variable will accumulate the running total cost of the whole order. +let totalCost = 0; + +// Loop through each item object in the order array. +// We use object destructuring in the for...of loop parameter to extract +// itemName, quantity, and unitPricePence from each object directly. +for (const { itemName, quantity, unitPricePence } of order) { + // Calculate the total cost of this line item. + // unitPricePence is in pence, so we divide by 100 to convert to pounds. + const itemTotal = (quantity * unitPricePence) / 100; + + // Add this item's total to the running grand total. + totalCost += itemTotal; + + // Print the row with aligned columns using padEnd. + // String(quantity) converts the number to a string so we can call padEnd on it. + // toFixed(2) formats the number to always show exactly 2 decimal places. + console.log( + String(quantity).padEnd(8) + itemName.padEnd(20) + itemTotal.toFixed(2) + ); +} + +// Print a blank line, then the total. +// The \n inside the template literal inserts a blank line before "Total:". +console.log(`\nTotal: ${totalCost.toFixed(2)}`);