From 7abb2384fa242121d0e51b8c324e64546bd82936 Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Fri, 27 Feb 2026 20:14:58 +0000 Subject: [PATCH 01/10] complete exercise 1 --- Sprint-1/destructuring/exercise-1/exercise.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..d86bc7bf 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}.` ); From c0ad8e9f25b0dbf9050a0de7d4f549fd5a7a557c Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Fri, 27 Feb 2026 20:47:19 +0000 Subject: [PATCH 02/10] hogwarts exercise done --- Sprint-1/destructuring/exercise-2/exercise.js | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..206fdcc3 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,39 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +function logPeopleByHouse(peopleList, houseName) { + peopleList.forEach((person) => { + if (person.house === houseName) { + const { firstName, lastName } = person; + console.log(`${firstName} ${lastName}`); + } + }); +} + +function logTeachersWithPets(peopleList) { + peopleList.forEach((person) => { + if (person.occupation === "Teacher" && person.pet !== null) { + const { firstName, lastName } = person; + console.log(`${firstName} ${lastName}`); + } + }); +} + +// I thought I'd make a more general version which can work for any condition +// though there are no checks for invalid input +function getPersonByPredicate(list, predicate) { + list.forEach((person) => { + if (predicate(person)) { + const { firstName, lastName } = person; + console.log(`${firstName} ${lastName}`); + } + }); +} + +getPersonByPredicate(hogwarts, (person) => person.house === "Gryffindor"); + +getPersonByPredicate( + hogwarts, + (person) => person.occupation === "Teacher" && person.pet +); From e0e402009073271099bea4a317551b2e6caa16f1 Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Fri, 27 Feb 2026 21:04:12 +0000 Subject: [PATCH 03/10] complete receipt exercise --- Sprint-1/destructuring/exercise-3/exercise.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..0cdd893c 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,18 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +function printReceipt(order) { + console.log(`${"QTY".padEnd(8)}${"ITEM".padEnd(20)}TOTAL`); + let total = 0; + order.forEach((item) => { + const { quantity, itemName, unitPricePence } = item; + console.log( + `${String(quantity).padEnd(8)}${String(itemName).padEnd(20)}${((quantity * unitPricePence) / 100).toFixed(2)}` + ); + total += quantity * unitPricePence; + }); + console.log(`\nTotal: ${(total / 100).toFixed(2)}`); +} + +printReceipt(order); From 84ebbefb9180a66155537e3370620759cef9d74d Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Fri, 27 Feb 2026 21:09:17 +0000 Subject: [PATCH 04/10] destructuring with default values --- Sprint-1/destructuring/exercise-1/exercise.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index d86bc7bf..8a74a963 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -6,7 +6,11 @@ const personOne = { // Update the parameter to this function to make it work. // Don't change anything else. -function introduceYourself({ name, age, favouriteFood }) { +function introduceYourself({ + name = "NO NAME", + age = "NO AGE", + favouriteFood = "NO FOOD", +}) { console.log( `Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.` ); From 1bdaae2127375b4e2fb755177a83eba63f27f68c Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Fri, 8 May 2026 15:53:45 +0100 Subject: [PATCH 05/10] remove dead code, show example usage of general purpose function --- Sprint-1/destructuring/exercise-2/exercise.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index 206fdcc3..bb2057ed 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -100,9 +100,15 @@ function getPersonByPredicate(list, predicate) { }); } +/* +Example usable of the above general purpose version which allows the user to filter by different +properties. + getPersonByPredicate(hogwarts, (person) => person.house === "Gryffindor"); getPersonByPredicate( hogwarts, (person) => person.occupation === "Teacher" && person.pet ); + +*/ From 5cd7bafcdd0a0ba5e7e8a4249a6f64a71a11a5c1 Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Thu, 14 May 2026 17:57:56 +0100 Subject: [PATCH 06/10] removed default values --- Sprint-1/destructuring/exercise-1/exercise.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 8a74a963..d86bc7bf 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -6,11 +6,7 @@ const personOne = { // Update the parameter to this function to make it work. // Don't change anything else. -function introduceYourself({ - name = "NO NAME", - age = "NO AGE", - favouriteFood = "NO FOOD", -}) { +function introduceYourself({ name, age, favouriteFood }) { console.log( `Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.` ); From dcb2eaa260b1cb89740c154875355c5e6fe24099 Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Thu, 14 May 2026 18:07:01 +0100 Subject: [PATCH 07/10] use more destructuring of objects --- Sprint-1/destructuring/exercise-2/exercise.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index bb2057ed..09ce7429 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -72,18 +72,16 @@ let hogwarts = [ ]; function logPeopleByHouse(peopleList, houseName) { - peopleList.forEach((person) => { - if (person.house === houseName) { - const { firstName, lastName } = person; + peopleList.forEach(({ house, firstName, lastName }) => { + if (house === houseName) { console.log(`${firstName} ${lastName}`); } }); } function logTeachersWithPets(peopleList) { - peopleList.forEach((person) => { - if (person.occupation === "Teacher" && person.pet !== null) { - const { firstName, lastName } = person; + peopleList.forEach(({ occupation, pet, firstName, lastName }) => { + if (occupation === "Teacher" && pet !== null) { console.log(`${firstName} ${lastName}`); } }); @@ -99,7 +97,6 @@ function getPersonByPredicate(list, predicate) { } }); } - /* Example usable of the above general purpose version which allows the user to filter by different properties. From f52238d429302b104ac24faf8dccd2eb4fe90336 Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Thu, 14 May 2026 18:29:09 +0100 Subject: [PATCH 08/10] use descructuring in the paramter of forEach, and make string padding more dynamic. Don't have to edit the string, just update the padding variables. --- Sprint-1/destructuring/exercise-3/exercise.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index 0cdd893c..5c7d1311 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -8,12 +8,14 @@ let order = [ ]; function printReceipt(order) { + const qtyWidth = 8; + const itemWidth = 20; + console.log(`${"QTY".padEnd(8)}${"ITEM".padEnd(20)}TOTAL`); let total = 0; - order.forEach((item) => { - const { quantity, itemName, unitPricePence } = item; + order.forEach(({ quantity, itemName, unitPricePence }) => { console.log( - `${String(quantity).padEnd(8)}${String(itemName).padEnd(20)}${((quantity * unitPricePence) / 100).toFixed(2)}` + `${String(quantity).padEnd(qtyWidth)}${String(itemName).padEnd(itemWidth)}${((quantity * unitPricePence) / 100).toFixed(2)}` ); total += quantity * unitPricePence; }); From 58566d9aed55b9db0a2b131d644386d4948a0871 Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Fri, 15 May 2026 15:50:39 +0100 Subject: [PATCH 09/10] move receipt string building to a helper function --- Sprint-1/destructuring/exercise-3/exercise.js | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index 5c7d1311..2d6eb42f 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -7,18 +7,28 @@ let order = [ { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; -function printReceipt(order) { +// helper function that pads each item in a line by a certain amount +function getReceiptString(quantity, itemName, total) { const qtyWidth = 8; const itemWidth = 20; - console.log(`${"QTY".padEnd(8)}${"ITEM".padEnd(20)}TOTAL`); + return `${String(quantity).padEnd(qtyWidth)}${itemName.padEnd(itemWidth)}${total}`; +} + +function printReceipt(order) { + // print header + console.log(getReceiptString("QTY", "ITEM", "TOTAL")); + let total = 0; + + // print each line item order.forEach(({ quantity, itemName, unitPricePence }) => { - console.log( - `${String(quantity).padEnd(qtyWidth)}${String(itemName).padEnd(itemWidth)}${((quantity * unitPricePence) / 100).toFixed(2)}` - ); + const price = ((quantity * unitPricePence) / 100).toFixed(2); + console.log(getReceiptString(quantity, itemName, price)); total += quantity * unitPricePence; }); + + // print total console.log(`\nTotal: ${(total / 100).toFixed(2)}`); } From 2a01028edaf54c9eaafd6ae328ff3c1aca8c4016 Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Fri, 15 May 2026 16:10:50 +0100 Subject: [PATCH 10/10] rename helper function to be more accurate --- Sprint-1/destructuring/exercise-3/exercise.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index 2d6eb42f..f9444012 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -8,7 +8,7 @@ let order = [ ]; // helper function that pads each item in a line by a certain amount -function getReceiptString(quantity, itemName, total) { +function formatReceiptEntry(quantity, itemName, total) { const qtyWidth = 8; const itemWidth = 20; @@ -17,14 +17,14 @@ function getReceiptString(quantity, itemName, total) { function printReceipt(order) { // print header - console.log(getReceiptString("QTY", "ITEM", "TOTAL")); + console.log(formatReceiptEntry("QTY", "ITEM", "TOTAL")); let total = 0; // print each line item order.forEach(({ quantity, itemName, unitPricePence }) => { const price = ((quantity * unitPricePence) / 100).toFixed(2); - console.log(getReceiptString(quantity, itemName, price)); + console.log(formatReceiptEntry(quantity, itemName, price)); total += quantity * unitPricePence; });