Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.
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 array-destructuring/exercise-1/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}.`
);
Expand Down
1 change: 1 addition & 0 deletions array-destructuring/exercise-1/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
15 changes: 15 additions & 0 deletions array-destructuring/exercise-2/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
function isGryffindor(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);
});
}
Comment on lines +1 to +10

Choose a reason for hiding this comment

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

Good attempt!

It looks like you are almost there, but be careful when reading requirements, you did not need to log the house in either task, also the first and last name needed to be separated by a space. This code will print HarryPotterGryffindor rather than Harry Potter.

Other improvements:

I would argue however that Array.map() is not the most appropriate method for this task.

Array.map() creates a new instance of an array and returns a value on every iteration, and in this case, you are not assigning the value of this new array to a variable, secondly, you are not returning anything inside the callback function (the function passed into Array.map()).

This means if you assigned the new array to a variable it would just be an array with X number of null values.

In my opinion, there are two improved ways to do this:

First (cleanest):

  list
       .filter( person => person.house === "Gryffindor" )
       .forEach( person => console.log(`${person.firstName} ${person.lastName}`) );

Article on Array.filter() method

Second:

  list.forEach( person => {
     if ( person.house === "Gryffindor" ) 
     {
         console.log(`${person.firstName} ${person.lastName}`)
     }
  } );

Copy link
Author

Choose a reason for hiding this comment

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

Thank you very much Jack, using methods one after another is really simple and effective usage. I didn't think about it before.

I see the difference between map and forEach better now. I always mixed those two.


let hogwarts = [
{
firstName: "Harry",
Expand Down Expand Up @@ -70,3 +81,7 @@ let hogwarts = [
occupation: "Teacher",
},
];

//isGryffindor(hogwarts);

havePet(hogwarts);
14 changes: 13 additions & 1 deletion array-destructuring/exercise-3/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment on lines +10 to +20

Choose a reason for hiding this comment

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

Good job!

Also, an improvement you could make to this solution would be to use Array.reduce() to create a running aggregate instead of defining totalPrice outside of the loop.

Here is a link that explains using reduce to calculate a sum.

https://linuxhint.com/call-reduce-on-an-array-of-objects-to-sum-their-properties/

Copy link
Author

Choose a reason for hiding this comment

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

that's a really useful method. I wish I knew the importance of code review and learn from it long before. Thank you very much for your brilliant and constructive feedback. Sorry for replying after a month. I never knew learning from code review would be this effective. 🙏