diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..d86bc7bf 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -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}.` ); diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..52e6c723 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,33 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +// Task 1 + +// In `exercise.js` 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. + +const gryffindor = hogwarts.filter(({ house }) => house === "Gryffindor"); + +console.log("Hogwarts people in Gryffindor"); +for (const person of gryffindor.map( + ({ firstName, lastName }) => `${firstName} ${lastName}` +)) { + console.log(person); +} + +console.log("\n"); + +// Task 2 +// In exercise.js write a program that will take the hogwarts array as input and display the names of teachers who have pets. +// Use object destructuring to extract the values you need out of each element in the array. + +console.log("Hogwarts teachers with pets"); +const teachersWithPets = hogwarts.filter(({ occupation, pet }) => { + return occupation === "Teacher" && pet; +}); + +for (const { firstName, lastName } of teachersWithPets) { + console.log(`${firstName} ${lastName}`); +}; \ No newline at end of file diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..6d247107 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,33 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +// In exercise.js write a program that will take the `order` array as input and print out a receipt for the order. +// Log each individual item to the console. +// Log the total cost of the order to the console. +// Use object destructuring to access the values you need from each item. +// Pay attention to the exact formatting of the expected result. + +// 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 total = 0; +console.log("QTY, ITEM, TOTAL"); +for (const { itemName, quantity, unitPricePence } of order) { + const itemTotal = quantity * unitPricePence / 100 + total += itemTotal; + console.log( + `${quantity} ${itemName} ${(itemTotal).toFixed(2)}` + ); +} + +console.log(`\nTotal: ${(total).toFixed(2)}`);