-
-
Notifications
You must be signed in to change notification settings - Fork 275
London | 26-ITP-May | Gideon Defar | Sprint 1 | Object Destructuring #557
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 all commits
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,36 @@ let order = [ | |
| { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, | ||
| { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, | ||
| ]; | ||
|
|
||
| function printReceipt(array) { | ||
| const qty = "QTY"; | ||
| const item = "ITEM"; | ||
| const totals = "TOTAL"; | ||
| console.log(`${qty.padEnd(10, " ")}${item.padEnd(20, " ")}${totals}`); | ||
|
|
||
| let totalPrice = 0; | ||
|
|
||
| for (const { itemName, quantity, unitPricePence } of array) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Destructing in the loop header is a very clean solution for this exercise. |
||
| const paddedQuantity = String(quantity).padEnd(10, " "); | ||
| const paddedItem = itemName.padEnd(20, " "); | ||
| let rowTotal = (quantity * unitPricePence) / 100; | ||
| let rowTotalDisplayed = rowTotal.toFixed(2); | ||
|
|
||
| console.log(`${paddedQuantity}${paddedItem}${rowTotalDisplayed}`); | ||
|
|
||
| totalPrice = totalPrice + rowTotal; | ||
| } | ||
| console.log(""); | ||
| console.log(`Total: ${totalPrice.toFixed(2)}`); | ||
| } | ||
| printReceipt(order); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you manage to verify that output formatting exactly matches the expected result formatting? |
||
| // Output: | ||
| // QTY ITEM TOTAL | ||
| // 1 Hot Cakes 2.32 | ||
| // 2 Apple Pie 2.78 | ||
| // 1 Egg McMuffin 2.80 | ||
| // 1 Sausage McMuffin 3.00 | ||
| // 2 Hot Coffee 2.00 | ||
| // 4 Hash Brown 1.60 | ||
|
|
||
| // Total: 14.50 | ||
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.
How does the output of this exercise look like?