Skip to content

Commit b172ead

Browse files
committed
Refactor column formatting function for receipt alignment
1 parent 579ae44 commit b172ead

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ let order = [
1111
const COLUMN_SPACING = 8;
1212
const ITEM_COLUMN_WIDTH = 20;
1313

14-
function formatColumn(content, width) {
15-
return content.toString().padEnd(width);
14+
// Function to format multiple columns
15+
function formatColumns(quantity, item, total) {
16+
return (
17+
quantity.toString().padEnd(COLUMN_SPACING) +
18+
item.padEnd(ITEM_COLUMN_WIDTH) +
19+
total
20+
);
1621
}
1722

1823
// Print the receipt header with aligned columns
19-
console.log(
20-
formatColumn("QTY", COLUMN_SPACING) +
21-
formatColumn("ITEM", ITEM_COLUMN_WIDTH) +
22-
"TOTAL"
23-
);
24+
console.log(formatColumns("QTY", "ITEM", "TOTAL"));
2425

2526
let totalCost = 0;
2627

@@ -29,11 +30,8 @@ order.forEach(({ itemName, quantity, unitPricePence }) => {
2930
totalCost += totalItemCost;
3031

3132
// Log each item's details
32-
console.log(
33-
formatColumn(quantity, COLUMN_SPACING) +
34-
formatColumn(itemName, ITEM_COLUMN_WIDTH) +
35-
totalItemCost.toFixed(2)
36-
);
33+
console.log(formatColumns(quantity, itemName, totalItemCost.toFixed(2)));
3734
});
3835

36+
// Print the total cost at the end
3937
console.log(`\nTotal: ${totalCost.toFixed(2)}`);

0 commit comments

Comments
 (0)