diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..7bd6c79b 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} = personOne) { 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..80c776df 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,30 @@ let hogwarts = [ occupation: "Teacher", }, ]; + + + +function displayGryffindor(hogwarts) { + let list = []; + for (const obj of hogwarts) { + const {firstName, lastName, house} = obj; + if (house === "Gryffindor") { + list.push(`${firstName} ${lastName}`) + } + } + return list; +} +console.log(displayGryffindor(hogwarts)); + + +function displayPetsOwners(hogwarts) { + let list = []; + for (const obj of hogwarts) { + const {firstName, lastName, pet, occupation} = obj; + if (pet && occupation === "Teacher") { + list.push(`${firstName} ${lastName}`) + } + } + return list; +} +console.log(displayPetsOwners(hogwarts)); diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..cd11824e 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,15 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +function printOrder(order) { + let printOut = "QTY".padEnd(8, " ") + "ITEM".padEnd(20, " ") + "TOTAL\n"; + let total = 0; + for (const { itemName, quantity, unitPricePence } of order) { + printOut += `${String(quantity).padEnd(8, " ")}${itemName.padEnd(20, " ")}${((quantity * unitPricePence) / 100).toFixed(2)}\n`; + total += unitPricePence * quantity; + } + printOut += `Total: ${(total / 100).toFixed(2)}`; + return printOut; +} +console.log(printOrder(order));