diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..1ef36775 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -1,15 +1,16 @@ const personOne = { - name: "Popeye", + firstName: "Popeye", age: 34, favouriteFood: "Spinach", }; +let { firstName, age, favouriteFood } = personOne; // Update the parameter to this function to make it work. // Don't change anything else. -function introduceYourself(___________________________) { +function introduceYourself() { console.log( - `Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.` + `Hello, my name is ${firstName}. I am ${age} years old and my favourite food is ${favouriteFood}.` ); } -introduceYourself(personOne); +console.log(introduceYourself(personOne)); diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..6cec9155 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,27 @@ let hogwarts = [ occupation: "Teacher", }, ]; +function gryffindorforStudent() { + let allStudents = ""; + for (let student of hogwarts) { + let { firstName, lastName, house, pet, occupation } = student; + if (house === "Gryffindor") { + allStudents += `${firstName} ${lastName}\n`; + } + } + return allStudents; +} + +function teacherWithPet() { + let allTeachers = ""; + for (const person of hogwarts) { + let { firstName, lastName, pet, occupation } = person; + if (occupation === "Teacher" && pet !== null) { + allTeachers += `${firstName} ${lastName}\n`; + } + } + return allTeachers; +} + +console.log(teacherWithPet(hogwarts)); +console.log(gryffindorforStudent(hogwarts)); diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..7a6fbd43 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,22 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +function receipt(order) { + let totalPrice = 0; + + console.log("QTY".padEnd(10) + "Item".padEnd(20) + "TOTAL"); + for (const item of order) { + let { itemName, quantity, unitPricePence } = item; + let pricePerQuantity = quantity * unitPricePence; + console.log( + String(quantity).padEnd(10) + + itemName.padEnd(20) + + (pricePerQuantity / 100).toFixed(2) + ); + totalPrice += pricePerQuantity; + } + totalPrice = (totalPrice / 100).toFixed(2); +} + +console.log(receipt(order));