From c44a24fcb7ab83003bcfd561ad145260d6e15e33 Mon Sep 17 00:00:00 2001 From: Della-Bella Date: Tue, 24 Dec 2024 23:41:44 +0000 Subject: [PATCH 1/4] Update the parameter to {name, age, favouriteFood} --- 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..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}.` ); From 4d871c41dc0e77fb65a670556235786d06f81b87 Mon Sep 17 00:00:00 2001 From: Della-Bella Date: Tue, 24 Dec 2024 23:55:39 +0000 Subject: [PATCH 2/4] destructuring the object personOne --- Sprint-1/destructuring/exercise-1/readme.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sprint-1/destructuring/exercise-1/readme.md b/Sprint-1/destructuring/exercise-1/readme.md index 28ca6c3d..eb855614 100644 --- a/Sprint-1/destructuring/exercise-1/readme.md +++ b/Sprint-1/destructuring/exercise-1/readme.md @@ -29,5 +29,9 @@ console.log(`Batman is ${firstName}, ${lastName}`); # Exercise -- What is the syntax to destructure the object `personOne` in exercise.js? +- What is the syntax to What is the syntax to destructure the object `personOne` in exercise.js? the object `personOne` in exercise.js? + +const { name, age, favouriteFood} = personOne; + + - Update the parameter of the function `introduceYourself` to use destructuring on the object that gets passed in. From 375f1991786c6a5c55d8772f773f87e70e8d710d Mon Sep 17 00:00:00 2001 From: Della-Bella Date: Fri, 27 Dec 2024 16:44:34 +0000 Subject: [PATCH 3/4] Destructuring Hogwarts and Pet functions --- Sprint-1/destructuring/exercise-2/exercise.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..0635dd84 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,34 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +function schoolName(hogwarts) { + for (let i = 0; i < hogwarts.length; i++) { + + // Access each object in the array + let { firstName, lastName, house } = hogwarts[i]; + + // Check if the house is Gryffindor + + if (house === "Gryffindor") { + console.log(`${firstName} ${lastName}`); + } + } +} + +function teachesPet(hogwarts) { + for (let i=0; i< hogwarts.length; i++){ + // Access object in the array + let { firstName, lastName, pet } = hogwarts[i]; //[i] loop through an array and access each item one by one + + // Check if the pet is valid + if (pet) { + console.log(`${firstName} ${lastName} has a: ${pet}`); + } + } + +} + +// Call the function +schoolName(hogwarts); +teachesPet(hogwarts); From 4eec7dd24ba9935a0cf1281ce1c9574721611b06 Mon Sep 17 00:00:00 2001 From: Della-Bella Date: Fri, 27 Dec 2024 17:22:49 +0000 Subject: [PATCH 4/4] Function Destruction Order Receipt --- Sprint-1/destructuring/exercise-3/exercise.js | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..4e94bcc3 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -1,3 +1,18 @@ + + +/*## Expected result + +``` +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*/ + let order = [ { itemName: "Hot cakes", quantity: 1, unitPricePence: 232 }, { itemName: "Apple Pie", quantity: 2, unitPricePence: 139 }, @@ -6,3 +21,41 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +function orderReceipt() { + + console.log("QTY ITEM Total"); + + + // Loop through the order array + for (let i=0; i< order.length; i++){ + let { itemName, quantity, unitPricePence } = order[i]; + + console.log(`${quantity} ${itemName} ${unitPricePence} `); + + + } +}; + +orderReceipt(); + +function sumTotal() { + let total = 0; // Initialize total + + // Loop through the order array + for (let i = 0; i < order.length; i++) { + let { quantity, unitPricePence } = order[i]; + total += quantity * unitPricePence; + } + + return total; // Return the total cost in pence +} + +// Call the function +let totalCostPence = sumTotal(); + +// Convert to pounds and format as 2 decimal places +let totalCostPounds = (totalCostPence / 100).toFixed(2); + +// result +console.log(`Total cost: £${totalCostPounds}`); \ No newline at end of file