From 41ddcad4cf7b8e32ff70855d0d7a5bd3b7674689 Mon Sep 17 00:00:00 2001 From: Malusi Skunyana Date: Wed, 13 Aug 2025 18:00:39 +0200 Subject: [PATCH 1/4] Refactor introduceYourself to use object destructuring in parameters --- Sprint-1/destructuring/exercise-1/exercise.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..7b28eddb 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -4,9 +4,8 @@ const personOne = { favouriteFood: "Spinach", }; -// Update the parameter to this function to make it work. -// Don't change anything else. -function introduceYourself(___________________________) { +// Destructure directly in the parameter +function introduceYourself({ name, age, favouriteFood }) { console.log( `Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.` ); From efa5ce86d85112c2574880e081983ac1b771856e Mon Sep 17 00:00:00 2001 From: Malusi Skunyana Date: Wed, 13 Aug 2025 18:21:26 +0200 Subject: [PATCH 2/4] Filter and display Gryffindor members and teachers with pets using object destructuring --- Sprint-1/destructuring/exercise-2/exercise.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..de724ef6 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: Display names of people in Gryffindor +hogwarts.forEach(({ firstName, lastName, house }) => { + if (house === "Gryffindor") { + console.log(`${firstName} ${lastName}`); + } +}); + +console.log(""); // Just to separate the two outputs + +// Task 2: Display names of teachers who have pets +hogwarts.forEach(({ firstName, lastName, pet, occupation }) => { + if (occupation === "Teacher" && pet) { + console.log(`${firstName} ${lastName}`); + } +}); From 764f28ee02c121819e17c3f7ac275e08172e2c63 Mon Sep 17 00:00:00 2001 From: Malusi Skunyana Date: Wed, 13 Aug 2025 18:29:54 +0200 Subject: [PATCH 3/4] Print receipt with item totals and overall total using object destructuring --- Sprint-1/destructuring/exercise-3/exercise.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..5e4138be 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,19 @@ 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 }) => { + const itemTotal = (quantity * unitPricePence) / 100; // Convert pence to pounds/dollars + totalCost += itemTotal; + + // Format each line with spacing to match the expected output + console.log( + `${quantity} ${itemName.padEnd(18)}${itemTotal.toFixed(2)}` + ); +}); + +console.log(`\nTotal: ${totalCost.toFixed(2)}`); From d1dd571309c1b9d50d1b1d206912c3ba1a8ecb99 Mon Sep 17 00:00:00 2001 From: Malusi Skunyana Date: Fri, 15 Aug 2025 07:36:55 +0200 Subject: [PATCH 4/4] Refactor Exercise 3 to dynamically adjust column widths and ensure exact formatting for receipt output --- Sprint-1/destructuring/exercise-3/exercise.js | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index 5e4138be..3f88de06 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -1,5 +1,5 @@ let order = [ - { itemName: "Hot cakes", quantity: 1, unitPricePence: 232 }, + { itemName: "Hot Cakes", quantity: 1, unitPricePence: 232 }, { itemName: "Apple Pie", quantity: 2, unitPricePence: 139 }, { itemName: "Egg McMuffin", quantity: 1, unitPricePence: 280 }, { itemName: "Sausage McMuffin", quantity: 1, unitPricePence: 300 }, @@ -7,18 +7,40 @@ let order = [ { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; -console.log("QTY ITEM TOTAL"); +// Dynamically calculate column widths +const qtyWidth = Math.max( + "QTY".length, + ...order.map(({ quantity }) => quantity.toString().length) +); +const itemWidth = Math.max( + "ITEM".length, + ...order.map(({ itemName }) => itemName.length) +); +const totalWidth = Math.max( + "TOTAL".length, + ...order.map(({ quantity, unitPricePence }) => + ((quantity * unitPricePence) / 100).toFixed(2).length + ) +); + +// Print header +console.log( + `${"QTY".padEnd(qtyWidth + 2)}${"ITEM".padEnd(itemWidth + 2)}${"TOTAL".padStart(totalWidth)}` +); let totalCost = 0; +// Print each item order.forEach(({ itemName, quantity, unitPricePence }) => { - const itemTotal = (quantity * unitPricePence) / 100; // Convert pence to pounds/dollars + const itemTotal = (quantity * unitPricePence) / 100; totalCost += itemTotal; - // Format each line with spacing to match the expected output console.log( - `${quantity} ${itemName.padEnd(18)}${itemTotal.toFixed(2)}` + `${quantity.toString().padEnd(qtyWidth + 2)}${itemName.padEnd(itemWidth + 2)}${itemTotal + .toFixed(2) + .padStart(totalWidth)}` ); }); +// Print total console.log(`\nTotal: ${totalCost.toFixed(2)}`);