diff --git a/array-destructuring/exercise-1/exercise.js b/array-destructuring/exercise-1/exercise.js index a6eab299..d81323bc 100644 --- a/array-destructuring/exercise-1/exercise.js +++ b/array-destructuring/exercise-1/exercise.js @@ -4,7 +4,9 @@ const personOne = { favouriteFood: "Spinach", }; -function introduceYourself(___________________________) { +let { name, age, favouriteFood } = personOne; + +function introduceYourself() { 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..c279101f 100644 --- a/array-destructuring/exercise-2/exercise.js +++ b/array-destructuring/exercise-2/exercise.js @@ -70,3 +70,31 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +//Task 1 + +function findGryffindor(arr){ + arr.forEach(element => { + let {firstName, lastName, house, pet, occupation} = element; + + if(house === "Gryffindor"){ + console.log(firstName, lastName); + } + }); +} + +findGryffindor(hogwarts); + +// Task 2 + +function findTeachersWithPets(arr){ + arr.forEach(element => { + let {firstName, lastName, house, pet, occupation} = element; + + if(occupation === "Teacher" && pet !== null){ + console.log(firstName, lastName); + } + }); +} + +findTeachersWithPets(hogwarts) \ No newline at end of file diff --git a/array-destructuring/exercise-3/exercise.js b/array-destructuring/exercise-3/exercise.js index 0a01f8f0..14a869f5 100644 --- a/array-destructuring/exercise-3/exercise.js +++ b/array-destructuring/exercise-3/exercise.js @@ -6,3 +6,14 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPrice: 1.0 }, { itemName: "Hash Brown", quantity: 4, unitPrice: 0.4 }, ]; + +console.log("QTY\tITEM\t\t\tTOTAL"); + +order.forEach((item) => { + let { itemName, quantity, unitPrice } = item; + const total = (quantity * unitPrice).toFixed(2); + console.log(`${quantity}\t${itemName.padEnd(20)}${total}`); +}); + +const totalOrderCost = order.reduce((total, item) => total + item.quantity * item.unitPrice, 0).toFixed(2); +console.log(`\nTotal: ${totalOrderCost}`);