From 090543388e6b35b427210d09b6becdd0d60169c9 Mon Sep 17 00:00:00 2001 From: fikretellek Date: Sun, 25 Feb 2024 23:45:43 +0000 Subject: [PATCH 1/2] working on arr destruction --- array-destructuring/exercise-1/exercise.js | 2 +- array-destructuring/exercise-1/readme.md | 1 + array-destructuring/exercise-2/exercise.js | 7 +++++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/array-destructuring/exercise-1/exercise.js b/array-destructuring/exercise-1/exercise.js index a6eab299..5b4cf5c7 100644 --- a/array-destructuring/exercise-1/exercise.js +++ b/array-destructuring/exercise-1/exercise.js @@ -4,7 +4,7 @@ const personOne = { favouriteFood: "Spinach", }; -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/array-destructuring/exercise-1/readme.md b/array-destructuring/exercise-1/readme.md index c67ec4f9..ed7cca66 100644 --- a/array-destructuring/exercise-1/readme.md +++ b/array-destructuring/exercise-1/readme.md @@ -18,4 +18,5 @@ The program above will print `Batman is Bruce Wayne`. Notice how we use the `{}` # Exercise - What is the syntax to destructure the object `personOne` in exercise.js? +- { name, age, favouriteFood } - Update the argument of the function `introduceYourself` to use destructuring on the object that gets passed in. diff --git a/array-destructuring/exercise-2/exercise.js b/array-destructuring/exercise-2/exercise.js index e11b75eb..1259606f 100644 --- a/array-destructuring/exercise-2/exercise.js +++ b/array-destructuring/exercise-2/exercise.js @@ -1,3 +1,10 @@ +function isGryffindor(list) { + list.map(({ firstName, house }) => { + if (house === "Gryffindor") return [...list]; + }); +} +isGryffindor([...hogwarts]); + let hogwarts = [ { firstName: "Harry", From 13e7a0c78caf389cfa77385961793d189bf3efe9 Mon Sep 17 00:00:00 2001 From: fikretellek Date: Mon, 26 Feb 2024 00:11:12 +0000 Subject: [PATCH 2/2] ex 3 done --- array-destructuring/exercise-2/exercise.js | 14 +++++++++++--- array-destructuring/exercise-3/exercise.js | 14 +++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/array-destructuring/exercise-2/exercise.js b/array-destructuring/exercise-2/exercise.js index 1259606f..6f7a26f7 100644 --- a/array-destructuring/exercise-2/exercise.js +++ b/array-destructuring/exercise-2/exercise.js @@ -1,9 +1,13 @@ function isGryffindor(list) { - list.map(({ firstName, house }) => { - if (house === "Gryffindor") return [...list]; + list.map(({ firstName, lastName, house }) => { + if (house === "Gryffindor") console.log(firstName, lastName, house); + }); +} +function havePet(list) { + list.map(({ firstName, lastName, pet, occupation }) => { + if (pet && occupation === "Teacher") console.log(firstName, lastName, pet); }); } -isGryffindor([...hogwarts]); let hogwarts = [ { @@ -77,3 +81,7 @@ let hogwarts = [ occupation: "Teacher", }, ]; + +//isGryffindor(hogwarts); + +havePet(hogwarts); diff --git a/array-destructuring/exercise-3/exercise.js b/array-destructuring/exercise-3/exercise.js index 0a01f8f0..83dd6373 100644 --- a/array-destructuring/exercise-3/exercise.js +++ b/array-destructuring/exercise-3/exercise.js @@ -2,7 +2,19 @@ let order = [ { itemName: "Hot cakes", quantity: 1, unitPrice: 2.29 }, { itemName: "Apple Pie", quantity: 2, unitPrice: 1.39 }, { itemName: "Egg McMuffin", quantity: 1, unitPrice: 2.8 }, - { itemName: "Sausage McMuffin", quantity: 1, unitPrice: 3.0 }, + { itemName: "Sausage Muff", quantity: 1, unitPrice: 3.0 }, { itemName: "Hot Coffee", quantity: 2, unitPrice: 1.0 }, { itemName: "Hash Brown", quantity: 4, unitPrice: 0.4 }, ]; + +function printBill(list) { + let total = 0; + console.log("QTY", "\t", "ITEM", "\t\t", "TOTAL"); + list.map(({ quantity, itemName, unitPrice }) => { + console.log(quantity, "\t", itemName, "\t", unitPrice * quantity); + total += quantity * unitPrice; + }); + console.log("Total:", total); +} + +printBill(order);