Skip to content

London | Phone-Naing | Module-Data-Flows | WEEK-Destructuring-Array #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Sprint-1/destructuring/exercise-1/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions Sprint-1/destructuring/exercise-2/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
26 changes: 26 additions & 0 deletions Sprint-1/destructuring/exercise-3/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));