From 13227621c04e2cc027ed1e9f2e8c1a1d845b8ce4 Mon Sep 17 00:00:00 2001 From: cyf Date: Sat, 15 Aug 2026 10:28:41 +0100 Subject: [PATCH 1/3] Complete object destructuring exercises --- Sprint-1/destructuring/exercise-1/exercise.js | 4 +-- Sprint-1/destructuring/exercise-2/exercise.js | 16 +++++++++ Sprint-1/destructuring/exercise-3/exercise.js | 33 +++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5cf..faa9adf27 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -6,10 +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 }) { console.log( `Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.` ); } -introduceYourself(personOne); +introduceYourself(personOne); \ No newline at end of file diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb9..0dfb2afa2 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", }, ]; + +// - In `exercise.js` write a program that will take the `hogwarts` array as input and display the names of the people who belong to the Gryffindor house. +// - Use object destructuring to extract the values you need out of each element in the array. + +function findGryffindorMembers(array) { + let pplInGryffindor = []; + + for (const character of array) { + const { firstName, lastName, house } = character; + const fullName = `${firstName} ${lastName}`; + if (house === "Gryffindor") { + pplInGryffindor.push(fullName); + } + } + return pplInGryffindor; +} \ 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 b3a36f4e4..ea835758e 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,36 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +function printReceipt(array) { + const qty = "QTY"; + const item = "ITEM"; + const totals = "TOTAL"; + console.log(`${qty.padEnd(10, " ")}${item.padEnd(20, " ")}${totals}`); + + let totalPrice = 0; + + for (const { itemName, quantity, unitPricePence } of array) { + const paddedQuantity = String(quantity).padEnd(10, " "); + const paddedItem = itemName.padEnd(20, " "); + let rowTotal = (quantity * unitPricePence) / 100; + let rowTotalDisplayed = rowTotal.toFixed(2); + + console.log(`${paddedQuantity}${paddedItem}${rowTotalDisplayed}`); + + totalPrice = totalPrice + rowTotal; + } + console.log(""); + console.log(`Total: ${totalPrice.toFixed(2)}`); +} +printReceipt(order); +// ``` +// QTY ITEM TOTAL +// 1 Hot Cakes 2.32 +// 2 Apple Pie 2.78 +// 1 Egg McMuffin 2.80 +// 1 Sausage McMuffin 3.00 +// 2 Hot Coffee 2.00 +// 4 Hash Brown 1.60 + +// Total: 14.50 \ No newline at end of file From 64abd687b379352e0e2459c13d569d308e984504 Mon Sep 17 00:00:00 2001 From: cyf Date: Sat, 15 Aug 2026 17:44:52 +0100 Subject: [PATCH 2/3] Verify receipt output formatting --- Sprint-1/destructuring/exercise-3/exercise.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index ea835758e..33b045d5a 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -29,7 +29,7 @@ function printReceipt(array) { console.log(`Total: ${totalPrice.toFixed(2)}`); } printReceipt(order); -// ``` +// Output: // QTY ITEM TOTAL // 1 Hot Cakes 2.32 // 2 Apple Pie 2.78 From de3ab09a05828916b017a511f4423a83159314ba Mon Sep 17 00:00:00 2001 From: cyf Date: Sat, 15 Aug 2026 17:48:33 +0100 Subject: [PATCH 3/3] Display Gryffindor members --- Sprint-1/destructuring/exercise-2/exercise.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index 0dfb2afa2..dab261be7 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -85,4 +85,16 @@ function findGryffindorMembers(array) { } } return pplInGryffindor; -} \ No newline at end of file +} + +console.log(findGryffindorMembers(hogwarts)); + +/** output: +The function returns an array containing the five Gryffindor members: Harry Potter, Ron Weasley, Hermione Granger, Minerva McGonagall, and Albus Dumbledore. +[ +'Harry Potter', +'Ron Weasley', +'Hermione Granger', +'Minerva McGonagall', +'Albus Dumbledore' +] */ \ No newline at end of file