Skip to content

Commit 9ea5a08

Browse files
committed
Takeout order
1 parent 55b25e5 commit 9ea5a08

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

‎Sprint-1/destructuring/exercise-3/exercise.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,30 @@ let order = [
66
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
77
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
88
];
9+
10+
// Column widths (matching the expected output)
11+
const QTY_WIDTH = 8;
12+
const ITEM_WIDTH = 20;
13+
14+
// Print header
15+
console.log(`${"QTY".padEnd(QTY_WIDTH)}${"ITEM".padEnd(ITEM_WIDTH)}TOTAL`);
16+
17+
let totalOrder = 0;
18+
19+
// Process each item using object destructuring
20+
for (const { itemName, quantity, unitPricePence } of order) {
21+
const itemTotalPence = quantity * unitPricePence;
22+
const itemTotalPounds = (itemTotalPence / 100).toFixed(2);
23+
24+
// Print the line item
25+
console.log(
26+
`${quantity.toString().padEnd(QTY_WIDTH)}${itemName.padEnd(ITEM_WIDTH)}${itemTotalPounds}`
27+
);
28+
29+
// Accumulate total (parseFloat to convert string back to number)
30+
totalOrder += parseFloat(itemTotalPounds);
31+
}
32+
33+
// Print final total (fixed to 2 decimal places)
34+
console.log(`\nTotal: ${totalOrder.toFixed(2)}`);
35+

0 commit comments

Comments
 (0)