Skip to content

Commit 3bb3554

Browse files
committed
Refactor receipt printing for consistent formatting and alignment
1 parent c586205 commit 3bb3554

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@ let order = [
77
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
88
];
99

10-
// Print the receipt header
11-
console.log("QTY ITEM TOTAL");
10+
// Define column spacing for consistent formatting
11+
const columnSpacing = 8;
12+
13+
// Print the receipt header with aligned columns
14+
console.log("QTY".padEnd(columnSpacing) + "ITEM".padEnd(20) + "TOTAL");
1215

13-
// Initialize total cost
1416
let totalCost = 0;
1517

16-
// Loop through the order and log each item
1718
order.forEach(({ itemName, quantity, unitPricePence }) => {
1819
const totalItemCost = (unitPricePence * quantity) / 100; // Convert pence to pounds
19-
totalCost += totalItemCost; // Add to total cost
20+
totalCost += totalItemCost;
2021

21-
// Log the item details with proper formatting
22+
// Log each item's details with proper alignment
2223
console.log(
23-
`${quantity.toString().padEnd(8)}${itemName.padEnd(20)}${totalItemCost.toFixed(2)}`
24+
`${quantity.toString().padEnd(columnSpacing)}${itemName.padEnd(20)}${totalItemCost.toFixed(2)}`
2425
);
2526
});
2627

27-
// Log the total cost
28+
// Print the total cost at the end
2829
console.log(`\nTotal: ${totalCost.toFixed(2)}`);

0 commit comments

Comments
 (0)