-
-
Notifications
You must be signed in to change notification settings - Fork 105
WM | 25-ITP-May | Abdullah Saleh | Sprint 1 | Array Destructuring #245
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
base: main
Are you sure you want to change the base?
WM | 25-ITP-May | Abdullah Saleh | Sprint 1 | Array Destructuring #245
Conversation
const hogwartsTeachers = hogwarts | ||
.filter(({ occupation, pet }) => occupation === "Teacher" && pet) | ||
.map(function ({ firstName, lastName, pet }) { | ||
if (pet) { | ||
return `${firstName} ${lastName}`; | ||
} | ||
}) | ||
.join("\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor suggestion here: the if check in the map is no longer needed; the filter already does two things
- Filter out people who are not teachers
- Filter out people who don't have pets.
.filter(({ occupation, pet }) => occupation === "Teacher" && pet)
order.forEach(({ itemName, quantity, unitPricePence }) => { | ||
const itemTotal = ((quantity * unitPricePence)/100).toFixed(2); | ||
billTotal += Number(itemTotal) | ||
console.log(quantity.toString().padEnd(8), itemName.padEnd(18), itemTotal.padStart(5)); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could simplify this code by using Array.prototype.reduce instead of forEach with an external billTotal variable.
Reduce is designed to accumulate a value (like the total bill) while iterating over the array, which makes the intent of the code clearer and avoids mutating variables outside the loop.
For more details, you can check out the MDN docs here:
👉 MDN: Array.prototype.reduce
const GryffindorHouse = hogwarts | ||
.filter(({ house }) => house === "Gryffindor") | ||
.map(function ({ firstName, lastName }) { | ||
return `${firstName} ${lastName}`; | ||
}).join("\n"); | ||
|
||
console.log(GryffindorHouse); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor suggestion here,
You can just do a console.log in the map
const GryffindorHouse = hogwarts
.filter(({ house }) => house === "Gryffindor")
.map(function ({ firstName, lastName }) {
console.log(`${firstName} ${lastName}`)
})
const hogwartsTeachers = hogwarts | ||
.filter(({ occupation, pet }) => occupation === "Teacher" && pet) | ||
.map(function ({ firstName, lastName, pet }) { | ||
if (pet) { | ||
return `${firstName} ${lastName}`; | ||
} | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just replace the return with a console.log here also
const hogwartsTeachers = hogwarts
.filter(({ occupation, pet }) => occupation === "Teacher" && pet)
.map(function ({ firstName, lastName, pet }) {
if (pet) {
return `${firstName} ${lastName}`;
}
})
.join("\n");
Learners, PR Template
Self checklist
Changelist
Briefly explain your PR.
Questions
Ask any questions you have for your reviewer.