Skip to content
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

West Midlands | Danial Bashirzadeh | Module-Data-Flows | sprint1 | Destructuring #133

Open
wants to merge 4 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
2 changes: 1 addition & 1 deletion Sprint-1/destructuring/exercise-1/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const personOne = {

// Update the parameter to this function to make it work.
// Don't change anything else.
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}.`
);
Expand Down
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",
},
];


hogwarts.forEach(({ firstName, lastName, house }) => {
if (house === 'Gryffindor') {
console.log(`${firstName} ${lastName}`);
}
});


hogwarts.forEach(({ firstName, lastName, occupation, pet }) => {
if (occupation === 'Teacher' && pet) {
console.log(`${firstName} ${lastName}`);
}
});
14 changes: 14 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,17 @@ let order = [
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
];


let total = 0;

console.log("QTY ITEM TOTAL");

order.forEach(({ quantity, itemName, unitPricePence }) => {

const itemTotal = quantity * unitPricePence;
total += itemTotal;
console.log(`${quantity.toString().padEnd(8)}${itemName.padEnd(20)}${(itemTotal/100).toFixed(2)}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you changed one of these numbers (e.g. made itemName pad to 25 instead of 20), you'd have to change another line of code a well.

Can you think how you can make it so that if you changed one of these numbers, you wouldn't need t ochange anything else?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Daniel for the valuable feedback. You are right, I can use a constant or variable to define the padding values, instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're currently only using the values 8 and 20 in one place, so I don't think a constant or variable would help here? That would only help if you were writing down 8 and 20 in multiple places...

});

console.log(`\nTotal: ${(total/100).toFixed(2)}`);