diff --git a/Sprint-1/destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js index 1ff2ac5c..643bc3d7 100644 --- a/Sprint-1/destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -6,10 +6,18 @@ const personOne = { // Update the parameter to this function to make it work. // Don't change anything else. -function introduceYourself(___________________________) { +function introduceYourself({ name, age, favouriteFood }) { + // const {name,age,favouriteFood}=person console.log( `Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.` ); } introduceYourself(personOne); + +// # Exercise + +// - What is the syntax to destructure the object `personOne` in exercise.js? +// - Update the parameter of the function `introduceYourself` to use destructuring on the object that gets passed in. + +// PersonOne is calling the Object as paraameter of the function introduceYourself diff --git a/Sprint-1/destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js index e11b75eb..88c6e22e 100644 --- a/Sprint-1/destructuring/exercise-2/exercise.js +++ b/Sprint-1/destructuring/exercise-2/exercise.js @@ -70,3 +70,17 @@ let hogwarts = [ occupation: "Teacher", }, ]; +function task1() { + let result = hogwarts.filter(({ house }) => house === "Gryffindor"); + // return result.map((ele) => ele.firstName + " " + ele.lastName); + return result.map(({ firstName, lastName }) => firstName + " " + lastName); +} +function task2() { + let result = hogwarts.filter( + ({ pet, occupation, house }) => + pet !== null && occupation == "Teacher" && house == "Gryffindor" + ); + return result.map(({ firstName, lastName }) => firstName + " " + lastName); +} + +console.log(task2()); diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e..335200a9 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -6,3 +6,29 @@ let order = [ { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, ]; + +const newOrder = order.map(({ quantity, itemName, unitPricePence }) => ({ + quantity, + itemName, + unitPricePence: twoDigitNumber(unitPricePence * quantity), +})); + +function twoDigitNumber(value) { + return (value / 100).toFixed(2); +} +function totalResult(newOrder) { + return newOrder + .reduce((a, { unitPricePence }) => a + Number(unitPricePence), 0) + .toFixed(2); +} +// console.log(newOrder); +// console.log("QTY ITEM TOTAL"); +console.log("QTY".padEnd(10) + "ITEM".padEnd(20) + "TOTAL".padEnd(6)); +newOrder.map(({ quantity, itemName, unitPricePence }) => + console.log( + `${quantity.toString().padEnd(6)}${itemName + .toString() + .padEnd(20)}${unitPricePence.toString().padEnd(6)}` + ) +); +console.log("Total: " + totalResult(newOrder));