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
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
6 changes: 5 additions & 1 deletion Sprint-1/destructuring/exercise-1/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
31 changes: 31 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,34 @@ let hogwarts = [
occupation: "Teacher",
},
];

function schoolName(hogwarts) {
for (let i = 0; i < hogwarts.length; i++) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for loop is a legit approach. But now we have better ways to iterate over each element in the array in Javascript. Can you apply alternative method instead of for loop here?


// 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);
53 changes: 53 additions & 0 deletions Sprint-1/destructuring/exercise-3/exercise.js
Original file line number Diff line number Diff line change
@@ -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 },
Expand All @@ -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}`);