diff --git a/array-destructuring/exercise-1/exercise.js b/array-destructuring/exercise-1/exercise.js index a6eab299..5b4cf5c7 100644 --- a/array-destructuring/exercise-1/exercise.js +++ b/array-destructuring/exercise-1/exercise.js @@ -4,7 +4,7 @@ const personOne = { favouriteFood: "Spinach", }; -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/array-destructuring/exercise-1/readme.md b/array-destructuring/exercise-1/readme.md index c67ec4f9..ed7cca66 100644 --- a/array-destructuring/exercise-1/readme.md +++ b/array-destructuring/exercise-1/readme.md @@ -18,4 +18,5 @@ The program above will print `Batman is Bruce Wayne`. Notice how we use the `{}` # Exercise - What is the syntax to destructure the object `personOne` in exercise.js? +- { name, age, favouriteFood } - Update the argument of the function `introduceYourself` to use destructuring on the object that gets passed in. diff --git a/array-destructuring/exercise-2/exercise.js b/array-destructuring/exercise-2/exercise.js index e11b75eb..6f7a26f7 100644 --- a/array-destructuring/exercise-2/exercise.js +++ b/array-destructuring/exercise-2/exercise.js @@ -1,3 +1,14 @@ +function isGryffindor(list) { + list.map(({ firstName, lastName, house }) => { + if (house === "Gryffindor") console.log(firstName, lastName, house); + }); +} +function havePet(list) { + list.map(({ firstName, lastName, pet, occupation }) => { + if (pet && occupation === "Teacher") console.log(firstName, lastName, pet); + }); +} + let hogwarts = [ { firstName: "Harry", @@ -70,3 +81,7 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +//isGryffindor(hogwarts); + +havePet(hogwarts); diff --git a/array-destructuring/exercise-3/exercise.js b/array-destructuring/exercise-3/exercise.js index 0a01f8f0..83dd6373 100644 --- a/array-destructuring/exercise-3/exercise.js +++ b/array-destructuring/exercise-3/exercise.js @@ -2,7 +2,19 @@ let order = [ { itemName: "Hot cakes", quantity: 1, unitPrice: 2.29 }, { itemName: "Apple Pie", quantity: 2, unitPrice: 1.39 }, { itemName: "Egg McMuffin", quantity: 1, unitPrice: 2.8 }, - { itemName: "Sausage McMuffin", quantity: 1, unitPrice: 3.0 }, + { itemName: "Sausage Muff", quantity: 1, unitPrice: 3.0 }, { itemName: "Hot Coffee", quantity: 2, unitPrice: 1.0 }, { itemName: "Hash Brown", quantity: 4, unitPrice: 0.4 }, ]; + +function printBill(list) { + let total = 0; + console.log("QTY", "\t", "ITEM", "\t\t", "TOTAL"); + list.map(({ quantity, itemName, unitPrice }) => { + console.log(quantity, "\t", itemName, "\t", unitPrice * quantity); + total += quantity * unitPrice; + }); + console.log("Total:", total); +} + +printBill(order);