From 94cee526d076b7eb200d4ef282351519d3852335 Mon Sep 17 00:00:00 2001 From: Imran Mohamed Date: Fri, 14 Nov 2025 15:58:28 +0000 Subject: [PATCH 1/6] Completed exercise 1 tasks --- Sprint-1/destructuring/exercise-1/exercise.js | 2 +- Sprint-1/destructuring/exercise-1/readme.md | 3 +++ 2 files changed, 4 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-1/readme.md b/Sprint-1/destructuring/exercise-1/readme.md index 28ca6c3d..fb38340b 100644 --- a/Sprint-1/destructuring/exercise-1/readme.md +++ b/Sprint-1/destructuring/exercise-1/readme.md @@ -30,4 +30,7 @@ console.log(`Batman is ${firstName}, ${lastName}`); # Exercise - What is the syntax to destructure the object `personOne` in exercise.js? +```js +let {name, age, favouriteFood} = personOne +``` - Update the parameter of the function `introduceYourself` to use destructuring on the object that gets passed in. From 5040e75eb2f70d3f5d19debeb09c557fe3a5f212 Mon Sep 17 00:00:00 2001 From: Imran Mohamed Date: Fri, 14 Nov 2025 16:21:37 +0000 Subject: [PATCH 2/6] Completed exercise 2 --- Sprint-1/destructuring/exercise-2/exercise.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..37d03d4e 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,27 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +//task 1 +function getGryffindorMembers(arr) { + let gryffindorMembers = []; + arr.forEach(({ firstName, lastName, house }) => { + if (house === "Gryffindor") { + gryffindorMembers.push(`${firstName} ${lastName}`); + } + }); + return gryffindorMembers; +} +console.log(getGryffindorMembers(hogwarts)); + +//task 2 +function getTeachersWithPets(arr) { + let teachersWithPets = []; + arr.forEach(({ firstName, lastName, occupation, pet }) => { + if (occupation === "Teacher" && pet) { + teachersWithPets.push(`${firstName} ${lastName}`); + } + }); + return teachersWithPets; +} +console.log(getTeachersWithPets(hogwarts)); \ No newline at end of file From 88a530fdf86d89907731450b55c48c16a264a274 Mon Sep 17 00:00:00 2001 From: Imran Mohamed Date: Fri, 14 Nov 2025 16:37:46 +0000 Subject: [PATCH 3/6] Refactor functions to log results instead of returning values --- Sprint-1/destructuring/exercise-2/exercise.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index 37d03d4e..61f0bc3e 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -79,9 +79,10 @@ function getGryffindorMembers(arr) { gryffindorMembers.push(`${firstName} ${lastName}`); } }); - return gryffindorMembers; + console.log(gryffindorMembers.join('\n')); } -console.log(getGryffindorMembers(hogwarts)); + +getGryffindorMembers(hogwarts); //task 2 function getTeachersWithPets(arr) { @@ -91,6 +92,7 @@ function getTeachersWithPets(arr) { teachersWithPets.push(`${firstName} ${lastName}`); } }); - return teachersWithPets; + console.log(teachersWithPets.join('\n')); } -console.log(getTeachersWithPets(hogwarts)); \ No newline at end of file + +getTeachersWithPets(hogwarts); \ No newline at end of file From 4de83c5f70dc79c6c3b5f1aaf92847a5001930d0 Mon Sep 17 00:00:00 2001 From: Imran Mohamed Date: Fri, 14 Nov 2025 19:48:19 +0000 Subject: [PATCH 4/6] Implement printReceipt function to display order details and total cost --- Sprint-1/destructuring/exercise-3/exercise.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..a93f2f8f 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,22 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +function printReceipt(order){ + let total = 0 , qtyWidthMax = 3, itemWidthMax = 4 + const receiptLines = order.map(({itemName, quantity, unitPricePence}) => { + const itemTotalPounds = (quantity*unitPricePence/100); + qtyWidthMax = Math.max(qtyWidthMax, String(quantity).length); + itemWidthMax = Math.max(itemWidthMax, itemName.length); + return { itemName, quantity, itemTotalPounds }; + }) + console.log('QTY'.padEnd(qtyWidthMax+4), 'ITEM'.padEnd(itemWidthMax+3), 'TOTAL'); + receiptLines.forEach(({itemName, quantity, itemTotalPounds}) => { + total += itemTotalPounds; + console.log(String(quantity).padEnd(qtyWidthMax+4), itemName.padEnd(itemWidthMax+3), `${itemTotalPounds.toFixed(2)}`); + }) + console.log(`\nTotal: ${total.toFixed(2)}`); + +} + +printReceipt(order); \ No newline at end of file From d831ae868d8a185c2f8dc9b0daecaae9f2b6b5ec Mon Sep 17 00:00:00 2001 From: Imran Mohamed Date: Sun, 16 Nov 2025 15:20:41 +0000 Subject: [PATCH 5/6] Refactor getGryffindorMembers and getTeachersWithPets functions to use filter and map for improved readability --- Sprint-1/destructuring/exercise-2/exercise.js | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index 61f0bc3e..6e6eba75 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -73,26 +73,21 @@ let hogwarts = [ //task 1 function getGryffindorMembers(arr) { - let gryffindorMembers = []; - arr.forEach(({ firstName, lastName, house }) => { - if (house === "Gryffindor") { - gryffindorMembers.push(`${firstName} ${lastName}`); - } - }); - console.log(gryffindorMembers.join('\n')); + const gryffindorMembers = arr.filter(({ house }) => house === "Gryffindor").map(({ firstName, lastName }) => `${firstName} ${lastName}`).join('\n'); + + console.log(gryffindorMembers); } getGryffindorMembers(hogwarts); //task 2 function getTeachersWithPets(arr) { - let teachersWithPets = []; - arr.forEach(({ firstName, lastName, occupation, pet }) => { - if (occupation === "Teacher" && pet) { - teachersWithPets.push(`${firstName} ${lastName}`); - } - }); - console.log(teachersWithPets.join('\n')); + const teachersWithPets = arr.filter(({ occupation, pet }) => occupation === "Teacher" && pet).map(({ firstName, lastName }) => + `${firstName} ${lastName}` + ).join('\n'); + + + console.log(teachersWithPets); } getTeachersWithPets(hogwarts); \ No newline at end of file From 3ceb38c0d35a665908a4c5d5971af4575395acf3 Mon Sep 17 00:00:00 2001 From: Imran Mohamed Date: Sun, 16 Nov 2025 15:20:46 +0000 Subject: [PATCH 6/6] Refactor printReceipt function for improved readability --- Sprint-1/destructuring/exercise-3/exercise.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index a93f2f8f..67ccf173 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -8,9 +8,11 @@ let order = [ ]; function printReceipt(order){ - let total = 0 , qtyWidthMax = 3, itemWidthMax = 4 + let total = 0 + let qtyWidthMax = 3 + let itemWidthMax = 4 const receiptLines = order.map(({itemName, quantity, unitPricePence}) => { - const itemTotalPounds = (quantity*unitPricePence/100); + const itemTotalPounds = (quantity * (unitPricePence / 100)); qtyWidthMax = Math.max(qtyWidthMax, String(quantity).length); itemWidthMax = Math.max(itemWidthMax, itemName.length); return { itemName, quantity, itemTotalPounds };