diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..0fe0c0c7 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -1,9 +1,10 @@ + const personOne = { name: "Popeye", age: 34, favouriteFood: "Spinach", }; - + let {name,age,favouriteFood} = personOne; // Update the parameter to this function to make it work. // Don't change anything else. function introduceYourself(___________________________) { diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..dcad4cbc 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,40 @@ let hogwarts = [ occupation: "Teacher", }, ]; + + +function gryffindorOccupiers(obj) { + const result = []; + + for (let person of obj) { + const { firstName, lastName, house } = person; + + if (house === "Gryffindor") { + result.push(`${firstName} ${lastName}`); + } + } + + return result; +} + +console.log(gryffindorOccupiers(hogwarts)); + + +// task 2 + +function teachersWithPets(hogwarts) { + const result = []; + + for (let person of hogwarts) { + const { firstName, lastName, occupation, pet } = person; + + if (occupation === "Teacher" && pet) { + result.push(`${firstName} ${lastName}`); + } + } + + return result; +} + +console.log(teachersWithPets(hogwarts)); + diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..55eeedc4 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,24 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + + +console.log("QTY ITEM TOTAL"); + +let grandTotal = 0; + +for (let item of order) { + // Object destructuring + const { itemName, quantity, unitPricePence } = item; + const total = (unitPricePence / 100) * quantity; + grandTotal += total; // the price is in pence so divide by 100 to get pounce equivalent, + + // Format each line + const qtyStr = String(quantity).padEnd(7); + const itemStr = itemName.padEnd(20); + const totalStr = total.toFixed(2); + + console.log(`${qtyStr}${itemStr}${totalStr}`); +} + +console.log(`\nTotal: ${grandTotal.toFixed(2)}`); \ No newline at end of file