From 96ed5c901cd4e63083e528bf007cd0edbada11cc Mon Sep 17 00:00:00 2001 From: Samiuk Date: Wed, 13 Aug 2025 18:29:14 +0100 Subject: [PATCH 1/5] - Fix introduceYourself: destructure personOne in function parameter. --- Sprint-1/destructuring/exercise-1/exercise.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..e543bcda 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -6,7 +6,9 @@ const personOne = { // Update the parameter to this function to make it work. // Don't change anything else. -function introduceYourself(___________________________) { +function introduceYourself({ name, age, favouriteFood }) { + // Use the destructured variables in the console.log statement. + // Don't change anything else. console.log( `Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.` ); From b25fa413ac0de28987c2dde2c43d1112651b5d85 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Wed, 13 Aug 2025 18:53:54 +0100 Subject: [PATCH 2/5] - Answer the questions in the Readme File. - Use object destructuring in introduceYourself parameter --- Sprint-1/destructuring/exercise-1/exercise.js | 4 +-- Sprint-1/destructuring/exercise-1/readme.md | 26 +++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index e543bcda..6c2c9321 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -6,9 +6,9 @@ const personOne = { // Update the parameter to this function to make it work. // Don't change anything else. +// Use object destructuring in introduceYourself parameter function introduceYourself({ name, age, favouriteFood }) { - // Use the destructured variables in the console.log statement. - // Don't change anything else. + 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..f1be65b4 100644 --- a/Sprint-1/destructuring/exercise-1/readme.md +++ b/Sprint-1/destructuring/exercise-1/readme.md @@ -29,5 +29,27 @@ console.log(`Batman is ${firstName}, ${lastName}`); # Exercise -- What is the syntax to destructure the object `personOne` in exercise.js? -- Update the parameter of the function `introduceYourself` to use destructuring on the object that gets passed in. +1 - What is the syntax to destructure the object `personOne` in exercise.js? + +For the personOne example, the destructuring syntax would be: + +``` +let { name, age, favouriteFood } = personOne; + +That pulls the three properties out of personOne into separate variables. + +``` +2 - Update the parameter of the function `introduceYourself` to use destructuring on the object that gets passed in. + +``` +To update the parameter of introduceYourself function to use destructuring, I would write: + +function introduceYourself({ name, age, favouriteFood }) { + console.log( + `Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.` + ); +} +This way, the function immediately unpacks those properties from the object is passed in, without +needing separate variable assignments. + +``` From 283f63efcbb6c429860de3ebc5ad8e3fcb5abf0d Mon Sep 17 00:00:00 2001 From: Samiuk Date: Wed, 13 Aug 2025 19:21:16 +0100 Subject: [PATCH 3/5] - Write a function named displayGryffindors that takes an object. with properties `firstName`, `lastName`, `house`, `pet`, and `occupation` and display the names of the people who belong to the Gryffindor house. --- Sprint-1/destructuring/exercise-2/exercise.js | 16 ++++++++++++++++ Sprint-1/destructuring/exercise-2/readme.md | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..cf5f423b 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", }, ]; + + +// 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 displayGryffindors({ firstName, lastName, house, pet, occupation }) { + if (house !== "Gryffindor") return; + console.log(`Hello, my name is ${firstName} ${lastName}. I am a ${occupation} at Hogwarts and I belong to the ${house} house. My pet is a ${pet ? pet : 'none'}.`); +} +hogwarts.forEach(displayGryffindors); + +// Write a function named displayGryffindors that takes an object +// with properties `firstName`, `lastName`, `house`, `pet`, and `occupation` +// and display the names of the people who belong to the Gryffindor house. + diff --git a/Sprint-1/destructuring/exercise-2/readme.md b/Sprint-1/destructuring/exercise-2/readme.md index 64b5ab1c..d102aa0e 100644 --- a/Sprint-1/destructuring/exercise-2/readme.md +++ b/Sprint-1/destructuring/exercise-2/readme.md @@ -33,5 +33,18 @@ Albus Dumbledore ### Expected result ``` + +Hello, my name is Harry Potter. I am a Student at Hogwarts and I belong to the Gryffindor house. +I have a pet Owl. +Hello, my name is Ron Weasley. I am a Student at Hogwarts and I belong to the Gryffindor house. +I have a pet Scabbers. +Hello, my name is Hermione Granger. I am a Student at Hogwarts and I belong to the Gryffindor house. +I have a pet Cat. +Hello, my name is Minerva McGonagall. I am a Teacher at Hogwarts and I belong to the Gryffindor house. +I don't have a pet. +Hello, my name is Albus Dumbledore. I am a Teacher at Hogwarts and I belong to the Gryffindor house. +I have a pet Phoenix. + + Albus Dumbledore ``` From a6639ec96263c7def59a6ca50bbba53d04922467 Mon Sep 17 00:00:00 2001 From: Samiuk Date: Wed, 13 Aug 2025 20:10:12 +0100 Subject: [PATCH 4/5] - Create a function to print the order details using destructuring to access itemName, quantity, and unitPricePence. --- Sprint-1/destructuring/exercise-3/exercise.js | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..87c55666 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,54 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +// Print the header for the receipt + +console.log("QTY ITEM TOTAL"); + +// Function to print the order details +// Use destructuring to access itemName, quantity, and unitPricePence + +function printOrder(itemName, quantity, unitPricePence) { + + // Iterate over each item in the order + // Use forEach to loop through the order array + // Use destructuring to access itemName, quantity, and unitPricePence + // Calculate total price for each item + + order.forEach(({ itemName, quantity, unitPricePence }) => { + const totalPrice = (quantity * unitPricePence) / 100; + console.log( + `${quantity.toString().padStart(2)} ${itemName.padEnd(20)} £${totalPrice.toFixed(2)}` + ); + }); + + // Calculate total cost of the order + // Use destructuring to access quantity and unitPricePence + // Use reduce to sum up the total cost + // Format the total cost to two decimal places and prefix with a currency symbol + // Log the total cost to the console + // Use toFixed(2) to ensure two decimal places + // Use console.log to print the total cost + + const totalCost = order.reduce((acc, { quantity, unitPricePence }) => { + return acc + (quantity * unitPricePence); +}, 0) / 100; +console.log(`\nTotal: £${totalCost.toFixed(2)}`); +} +// Call the function to print the order +printOrder(order); + + + + +// 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 From 72f70efb5335ec36409abf0342ee9f6d4e9a7dea Mon Sep 17 00:00:00 2001 From: HoussamLh <156331030+HoussamLh@users.noreply.github.com> Date: Mon, 18 Aug 2025 15:29:56 +0100 Subject: [PATCH 5/5] exercise.js Improve Gryffindor display function for better grammar - Updated pet handling so that the sentence is grammatically correct. - Now only mentions a pet if one exists. - Matches the example style. --- Sprint-1/destructuring/exercise-2/exercise.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index cf5f423b..d10ee61d 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -77,10 +77,16 @@ let hogwarts = [ // Use object destructuring to extract the values you need out of each element in the array. function displayGryffindors({ firstName, lastName, house, pet, occupation }) { - if (house !== "Gryffindor") return; - console.log(`Hello, my name is ${firstName} ${lastName}. I am a ${occupation} at Hogwarts and I belong to the ${house} house. My pet is a ${pet ? pet : 'none'}.`); + if (house !== "Gryffindor") return; + + let petSentence = pet + ? ` I have a ${pet}.` + : ""; + + console.log( + `Hello, my name is ${firstName} ${lastName}. I am a ${occupation} at Hogwarts and I belong to the ${house} house.${petSentence}` + ); } -hogwarts.forEach(displayGryffindors); // Write a function named displayGryffindors that takes an object // with properties `firstName`, `lastName`, `house`, `pet`, and `occupation`