diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..7b28eddb 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -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}.` ); diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..de724ef6 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -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}`); + } +}); diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..3f88de06 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -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)}`);