Skip to content
Closed
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
4 changes: 3 additions & 1 deletion Sprint-1/destructuring/exercise-1/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const personOne = {

// Update the parameter to this function to make it work.
// Don't change anything else.
function introduceYourself(___________________________) {
// Use object destructuring in introduceYourself parameter
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
26 changes: 24 additions & 2 deletions Sprint-1/destructuring/exercise-1/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

```
22 changes: 22 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,25 @@ 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;

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}`
);
}

// 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.

13 changes: 13 additions & 0 deletions Sprint-1/destructuring/exercise-2/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
51 changes: 51 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,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