Skip to content

London | ITP-May-2025 | Mo Muchunu | Module-Data-Flows | Sprint 1 | Array Destructuring #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sprint-1/destructuring/exercise-1/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}.`
);
Expand Down
16 changes: 16 additions & 0 deletions Sprint-1/destructuring/exercise-2/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,19 @@ let hogwarts = [
occupation: "Teacher",
},
];


// Task 1 ---> List all Gryffindor house members
hogwarts
.filter(({ house }) => house === "Gryffindor") // Filter the array and keep only objects with 'house' matching 'Gryffindor'
.forEach(({ firstName, lastName }) => { // Loop through each filtered object in filtered array, and extract first and last names
console.log(`${firstName} ${lastName}`);
});


// Task 2 ---> names of teachers who have pets
for (const { firstName, lastName, occupation, pet } of hogwarts) { // Loop through array, destructuring 4 properties from each object
if (occupation === "Teacher" && pet) { // condition to check person's occupation and pet status
console.log(`${firstName} ${lastName}`);
}
}
13 changes: 13 additions & 0 deletions Sprint-1/destructuring/exercise-3/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,16 @@ let order = [
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
];

// Print receipt for takeout order
console.log("QTY ITEM TOTAL"); // Print receipt header row with even spacing for 3 columns

const orderTotal = order.reduce((sum, { itemName, quantity, unitPricePence }) => { // Using reduce() to process each item in 'order' array and sum() to tally total order cost
const itemPriceTotal = (unitPricePence * quantity) / 100; // Destructure for required unit price and quantity values from each object: calculate in then divide by 100 for total in pounds
console.log(
`${quantity.toString().padEnd(8)}${itemName.padEnd(20)}${itemPriceTotal.toFixed(2)}` // Add spacing to align row output neatly and format total prices to 2 decimal places
);
return sum + itemPriceTotal; // Add item's total price to the running sum
}, 0);

console.log(`\nTotal: ${orderTotal.toFixed(2)}`); // Print final total cost, formatted to 2 decimal places- on a new line