@@ -11,16 +11,17 @@ let order = [
1111const COLUMN_SPACING = 8 ;
1212const 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
2526let 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
3937console . log ( `\nTotal: ${ totalCost . toFixed ( 2 ) } ` ) ;
0 commit comments