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..09ce7429 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,42 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +function logPeopleByHouse(peopleList, houseName) { + peopleList.forEach(({ house, firstName, lastName }) => { + if (house === houseName) { + console.log(`${firstName} ${lastName}`); + } + }); +} + +function logTeachersWithPets(peopleList) { + peopleList.forEach(({ occupation, pet, firstName, lastName }) => { + if (occupation === "Teacher" && pet !== null) { + 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}`); + } + }); +} +/* +Example usable of the above general purpose version which allows the user to filter by different +properties. + +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..f9444012 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,30 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +// helper function that pads each item in a line by a certain amount +function formatReceiptEntry(quantity, itemName, total) { + const qtyWidth = 8; + const itemWidth = 20; + + return `${String(quantity).padEnd(qtyWidth)}${itemName.padEnd(itemWidth)}${total}`; +} + +function printReceipt(order) { + // print header + console.log(formatReceiptEntry("QTY", "ITEM", "TOTAL")); + + let total = 0; + + // print each line item + order.forEach(({ quantity, itemName, unitPricePence }) => { + const price = ((quantity * unitPricePence) / 100).toFixed(2); + console.log(formatReceiptEntry(quantity, itemName, price)); + total += quantity * unitPricePence; + }); + + // print total + console.log(`\nTotal: ${(total / 100).toFixed(2)}`); +} + +printReceipt(order);