diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..8a74a963 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -6,7 +6,11 @@ const personOne = { // Update the parameter to this function to make it work. // Don't change anything else. -function introduceYourself(___________________________) { +function introduceYourself({ + name = "NO NAME", + age = "NO AGE", + favouriteFood = "NO FOOD", +}) { 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..206fdcc3 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,39 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +function logPeopleByHouse(peopleList, houseName) { + peopleList.forEach((person) => { + if (person.house === houseName) { + const { firstName, lastName } = person; + console.log(`${firstName} ${lastName}`); + } + }); +} + +function logTeachersWithPets(peopleList) { + peopleList.forEach((person) => { + if (person.occupation === "Teacher" && person.pet !== null) { + const { firstName, lastName } = person; + console.log(`${firstName} ${lastName}`); + } + }); +} + +// I thought I'd make a more general version which can work for any condition +// though there are no checks for invalid input +function getPersonByPredicate(list, predicate) { + list.forEach((person) => { + if (predicate(person)) { + const { firstName, lastName } = person; + console.log(`${firstName} ${lastName}`); + } + }); +} + +getPersonByPredicate(hogwarts, (person) => person.house === "Gryffindor"); + +getPersonByPredicate( + hogwarts, + (person) => person.occupation === "Teacher" && person.pet +); diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..0cdd893c 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,18 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +function printReceipt(order) { + console.log(`${"QTY".padEnd(8)}${"ITEM".padEnd(20)}TOTAL`); + let total = 0; + order.forEach((item) => { + const { quantity, itemName, unitPricePence } = item; + console.log( + `${String(quantity).padEnd(8)}${String(itemName).padEnd(20)}${((quantity * unitPricePence) / 100).toFixed(2)}` + ); + total += quantity * unitPricePence; + }); + console.log(`\nTotal: ${(total / 100).toFixed(2)}`); +} + +printReceipt(order);