diff --git a/array-destructuring/exercise-1/exercise.js b/array-destructuring/exercise-1/exercise.js index a6eab299..04fd7aae 100644 --- a/array-destructuring/exercise-1/exercise.js +++ b/array-destructuring/exercise-1/exercise.js @@ -4,7 +4,8 @@ const personOne = { favouriteFood: "Spinach", }; -function introduceYourself(___________________________) { +function introduceYourself(person) { + const { name, age, favouriteFood } = person; console.log( `Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.` ); diff --git a/array-destructuring/exercise-2/exercise.js b/array-destructuring/exercise-2/exercise.js index e11b75eb..ddf2d2ae 100644 --- a/array-destructuring/exercise-2/exercise.js +++ b/array-destructuring/exercise-2/exercise.js @@ -70,3 +70,22 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +function showPeopleBelongIn(people, houseName) { + const peopleInHouse = people.filter((person) => person.house === houseName); + peopleInHouse.forEach((person) => { + const { firstName, lastName } = person; + console.log(`${firstName} ${lastName}`); + }) +} + +function showTeachersWithPets(people) { + const teachersWithPets = people.filter((person) => person.pet !== null && person.occupation === "Teacher"); + teachersWithPets.forEach((teacherWithPet) => { + const { firstName, lastName } = teacherWithPet; + console.log(`\n${firstName} ${lastName}`); + }) +} + +showPeopleBelongIn(hogwarts, "Gryffindor"); +showTeachersWithPets(hogwarts); diff --git a/array-destructuring/exercise-3/exercise.js b/array-destructuring/exercise-3/exercise.js index 0a01f8f0..29f01393 100644 --- a/array-destructuring/exercise-3/exercise.js +++ b/array-destructuring/exercise-3/exercise.js @@ -6,3 +6,29 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPrice: 1.0 }, { itemName: "Hash Brown", quantity: 4, unitPrice: 0.4 }, ]; + +function makeReceipt(order) { + let total = 0; + const itemNameArray = [] + const quantityArray = [] + const itemPriceArray = [] + + order.forEach((item) => { + const { itemName, quantity, unitPrice } = item; + total += quantity * unitPrice; + quantityArray.push(quantity); + itemNameArray.push(itemName); + itemPriceArray.push(quantity * unitPrice); + }) + + console.log(`QTY ITEM TOTAL`); + + itemNameArray.forEach((itemName, index) => { + const spaces = " ".repeat(20 - itemName.length); + console.log(`${quantityArray[index]} ${itemName}${spaces}$${itemPriceArray[index].toFixed(2)}`); + }) + + console.log(`\nTotal: $${total.toFixed(2)}`); +} + +makeReceipt(order); \ No newline at end of file