Skip to content
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
5 changes: 4 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,10 @@ const personOne = {

// Update the parameter to this function to make it work.
// Don't change anything else.
function introduceYourself(___________________________) {
function introduceYourself({ name, age, favouriteFood }) {
// The curly braces in the parameter tell JavaScript:
// "When this function receives an object, automatically extract
// name, age, and favouriteFood from it as separate variables."
console.log(
`Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.`
);
Expand Down
23 changes: 23 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,26 @@ let hogwarts = [
occupation: "Teacher",
},
];

// Task 1: Display names of all Gryffindor members.
// The "for...of" loop goes through each object in the array one by one.
// We use { firstName, lastName, house } to destructure only the three
// properties we need from each object — we ignore the rest.
for (const { firstName, lastName, house } of hogwarts) {
if (house === "Gryffindor") {
console.log(`${firstName} ${lastName}`);
}
}

// Task 2: Display names of teachers who have pets.
// (A blank line separates Task 1 output from Task 2 output in the console.)
// We destructure four properties this time: firstName, lastName,
// occupation, and pet.
// We check two conditions at once using the && (AND) operator:
// 1. occupation must be "Teacher"
// 2. pet must not be null (meaning they actually have a pet)
for (const { firstName, lastName, occupation, pet } of hogwarts) {
if (occupation === "Teacher" && pet !== null) {
console.log(`${firstName} ${lastName}`);
}
}
31 changes: 31 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,34 @@ let order = [
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
];

// padEnd(n) adds spaces after a string until it reaches n characters wide.
// This makes columns line up neatly in the console output.
// Example: "QTY".padEnd(8) gives "QTY " (5 spaces added, total 8 chars).
console.log("QTY".padEnd(8) + "ITEM".padEnd(20) + "TOTAL");

// This variable will accumulate the running total cost of the whole order.
let totalCost = 0;

// Loop through each item object in the order array.
// We use object destructuring in the for...of loop parameter to extract
// itemName, quantity, and unitPricePence from each object directly.
for (const { itemName, quantity, unitPricePence } of order) {
// Calculate the total cost of this line item.
// unitPricePence is in pence, so we divide by 100 to convert to pounds.
const itemTotal = (quantity * unitPricePence) / 100;

// Add this item's total to the running grand total.
totalCost += itemTotal;

// Print the row with aligned columns using padEnd.
// String(quantity) converts the number to a string so we can call padEnd on it.
// toFixed(2) formats the number to always show exactly 2 decimal places.
console.log(
String(quantity).padEnd(8) + itemName.padEnd(20) + itemTotal.toFixed(2)
);
}

// Print a blank line, then the total.
// The \n inside the template literal inserts a blank line before "Total:".
console.log(`\nTotal: ${totalCost.toFixed(2)}`);
Loading