West Midlands | 25-ITP-Sep | Ali Naru | Sprint 1 | feature/destructuring #333
West Midlands | 25-ITP-Sep | Ali Naru | Sprint 1 | feature/destructuring #333MohammedNaru wants to merge 1 commit intoCodeYourFuture:mainfrom
Conversation
…dor member and teacher retrieval functions
|
Your PR's title isn't in the expected format. Please check the expected title format, and update yours to match. Reason: Wrong number of parts separated by |s If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). If this PR needs reviewed, please add the 'Needs Review' label to this PR after you have resolved the issues listed above. |
|
Your PR description contained template fields which weren't filled in. Check you've ticked everything in the self checklist, and that any sections which prompt you to fill in an answer are either filled in or removed. If this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed). If this PR needs reviewed, please add the 'Needs Review' label to this PR after you have resolved the issues listed above. |
| ]; | ||
|
|
||
| function getGryffindorMembers(arr) { | ||
| const gryffindorMembers = arr.filter(({ house }) => house === "Gryffindor").map(({ firstName, lastName }) => `${firstName} ${lastName}`).join('\n'); |
There was a problem hiding this comment.
A long statement that chains multiple methods/functions becomes more readable when written across multiple lines, placing each method/function on its own line. For example,
const gryffindorMembers = arr
.filter(({ house }) => house === "Gryffindor")
.map(({ firstName, lastName }) => `${firstName} ${lastName}`)
.join('\n');| const receiptLines = order.map(({itemName, quantity, unitPricePence}) => { | ||
| const itemTotalPounds = (quantity * (unitPricePence / 100)); | ||
| qtyWidth = Math.max(qtyWidth, String(quantity).length); | ||
| itemWidth = Math.max(itemWidth, itemName.length); | ||
| return { itemName, quantity, itemTotalPounds }; | ||
| }) | ||
| console.log('QTY'.padEnd(qtyWidth+4), 'ITEM'.padEnd(itemWidth+3), 'TOTAL'); | ||
| receiptLines.forEach(({itemName, quantity, itemTotalPounds}) => { | ||
| total += itemTotalPounds; | ||
| console.log(String(quantity).padEnd(qtyWidth+4), itemName.padEnd(itemWidth+3), `${itemTotalPounds.toFixed(2)}`); | ||
| }) |
There was a problem hiding this comment.
Here is an alternative approach to express the code:
// Calculate reserved width first
const qtyWidth = 4 + Math.max(3, ...order.map(({quantity}) => String(quantity).length));
const itemWidth = 3 + Math.max(4, ...order.map({itemName}) => itemName.length));
// Prepare a helper function to construct a formatted string
const formatStr = (qty, name, total) =>
`${qty.padEnd(qtyWidth)}${name.padEnd(itemWidth)}${total}`);
console.log(formatStr('QTY', 'ITEM', 'TOTAL');
let total = 0;
order.forEach(({itemName, quantity, unitPricePence}) => {
const subtotal = quantity * unitPricePence / 100;
total += subtotal;
console.log(formatStr(quantity, itemName, subtotal.toFixed(2)));
})
Feature/destructuring Pull Request
Self checklist
Changelist
Refactored changes to the 3 exercise files
Questions
No Questions for reviewer.