-
-
Notifications
You must be signed in to change notification settings - Fork 142
London | 25-ITP-Sep | Imran Mohamed | Sprint 1 | Feature/destructuring #327
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?
Changes from 4 commits
94cee52
5040e75
88a530f
4de83c5
d831ae8
3ceb38c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,22 @@ let order = [ | |
| { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, | ||
| { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, | ||
| ]; | ||
|
|
||
| function printReceipt(order){ | ||
| let total = 0 , qtyWidthMax = 3, itemWidthMax = 4 | ||
|
||
| const receiptLines = order.map(({itemName, quantity, unitPricePence}) => { | ||
| const itemTotalPounds = (quantity*unitPricePence/100); | ||
|
||
| qtyWidthMax = Math.max(qtyWidthMax, String(quantity).length); | ||
| itemWidthMax = Math.max(itemWidthMax, itemName.length); | ||
| return { itemName, quantity, itemTotalPounds }; | ||
| }) | ||
| console.log('QTY'.padEnd(qtyWidthMax+4), 'ITEM'.padEnd(itemWidthMax+3), 'TOTAL'); | ||
| receiptLines.forEach(({itemName, quantity, itemTotalPounds}) => { | ||
| total += itemTotalPounds; | ||
| console.log(String(quantity).padEnd(qtyWidthMax+4), itemName.padEnd(itemWidthMax+3), `${itemTotalPounds.toFixed(2)}`); | ||
| }) | ||
| console.log(`\nTotal: ${total.toFixed(2)}`); | ||
|
|
||
| } | ||
|
|
||
| printReceipt(order); | ||
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.
As an extension: could you show me how you can use filter and map on an array to do this?
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.
Used filter and map for both functions in this exercise