Skip to content

Commit 579ae44

Browse files
committed
Add function for receipt formatting
1 parent 3bb3554 commit 579ae44

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

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

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

10-
// Define column spacing for consistent formatting
11-
const columnSpacing = 8;
10+
// Define constants for column formatting
11+
const COLUMN_SPACING = 8;
12+
const ITEM_COLUMN_WIDTH = 20;
13+
14+
function formatColumn(content, width) {
15+
return content.toString().padEnd(width);
16+
}
1217

1318
// Print the receipt header with aligned columns
14-
console.log("QTY".padEnd(columnSpacing) + "ITEM".padEnd(20) + "TOTAL");
19+
console.log(
20+
formatColumn("QTY", COLUMN_SPACING) +
21+
formatColumn("ITEM", ITEM_COLUMN_WIDTH) +
22+
"TOTAL"
23+
);
1524

1625
let totalCost = 0;
1726

1827
order.forEach(({ itemName, quantity, unitPricePence }) => {
1928
const totalItemCost = (unitPricePence * quantity) / 100; // Convert pence to pounds
2029
totalCost += totalItemCost;
2130

22-
// Log each item's details with proper alignment
31+
// Log each item's details
2332
console.log(
24-
`${quantity.toString().padEnd(columnSpacing)}${itemName.padEnd(20)}${totalItemCost.toFixed(2)}`
33+
formatColumn(quantity, COLUMN_SPACING) +
34+
formatColumn(itemName, ITEM_COLUMN_WIDTH) +
35+
totalItemCost.toFixed(2)
2536
);
2637
});
2738

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

0 commit comments

Comments
 (0)