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
5 changes: 2 additions & 3 deletions Sprint-1/destructuring/exercise-1/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ const personOne = {
favouriteFood: "Spinach",
};

// Update the parameter to this function to make it work.
// Don't change anything else.
function introduceYourself(___________________________) {
// Destructure directly in the 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
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: Display names of people in Gryffindor
hogwarts.forEach(({ firstName, lastName, house }) => {
if (house === "Gryffindor") {
console.log(`${firstName} ${lastName}`);
}
});

console.log(""); // Just to separate the two outputs

// Task 2: Display names of teachers who have pets
hogwarts.forEach(({ firstName, lastName, pet, occupation }) => {
if (occupation === "Teacher" && pet) {
console.log(`${firstName} ${lastName}`);
}
});
40 changes: 39 additions & 1 deletion Sprint-1/destructuring/exercise-3/exercise.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
let order = [
{ itemName: "Hot cakes", quantity: 1, unitPricePence: 232 },
{ itemName: "Hot Cakes", quantity: 1, unitPricePence: 232 },
{ itemName: "Apple Pie", quantity: 2, unitPricePence: 139 },
{ itemName: "Egg McMuffin", quantity: 1, unitPricePence: 280 },
{ itemName: "Sausage McMuffin", quantity: 1, unitPricePence: 300 },
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
];

// Dynamically calculate column widths
const qtyWidth = Math.max(
"QTY".length,
...order.map(({ quantity }) => quantity.toString().length)
);
const itemWidth = Math.max(
"ITEM".length,
...order.map(({ itemName }) => itemName.length)
);
const totalWidth = Math.max(
"TOTAL".length,
...order.map(({ quantity, unitPricePence }) =>
((quantity * unitPricePence) / 100).toFixed(2).length
)
);

// Print header
console.log(
`${"QTY".padEnd(qtyWidth + 2)}${"ITEM".padEnd(itemWidth + 2)}${"TOTAL".padStart(totalWidth)}`
);

let totalCost = 0;

// Print each item
order.forEach(({ itemName, quantity, unitPricePence }) => {
const itemTotal = (quantity * unitPricePence) / 100;
totalCost += itemTotal;

console.log(
`${quantity.toString().padEnd(qtyWidth + 2)}${itemName.padEnd(itemWidth + 2)}${itemTotal
.toFixed(2)
.padStart(totalWidth)}`
);
});

// Print total
console.log(`\nTotal: ${totalCost.toFixed(2)}`);