File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed
Sprint-1/destructuring/exercise-3 Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -6,3 +6,24 @@ let order = [
66 { itemName : "Hot Coffee" , quantity : 2 , unitPricePence : 100 } ,
77 { itemName : "Hash Brown" , quantity : 4 , unitPricePence : 40 } ,
88] ;
9+
10+ function getReceipt ( order ) {
11+ let total = 0 ;
12+ let receiptLines = [ "QTY ITEM TOTAL" ] ;
13+ let itemLines = order . map ( ( { itemName, quantity, unitPricePence } ) => {
14+ const itemTotal = quantity * unitPricePence ;
15+ total += itemTotal ;
16+ const priceInPounds = ( itemTotal / 100 ) . toFixed ( 2 ) ;
17+
18+ const quantityCol = String ( quantity ) . padEnd ( 3 , ' ' ) ;
19+ const itemCol = itemName . padEnd ( 20 , ' ' ) ;
20+ const priceCol = `${ priceInPounds } ` . padStart ( 6 , ' ' ) ;
21+
22+ return `${ quantityCol } ${ itemCol } ${ priceCol } ` ;
23+ } ) ;
24+ receiptLines = receiptLines . concat ( itemLines ) ;
25+ receiptLines . push ( "" ) ;
26+ receiptLines . push ( `TOTAL: ${ ( total / 100 ) . toFixed ( 2 ) } ` ) ;
27+ return receiptLines . join ( '\n' ) ;
28+ }
29+ console . log ( getReceipt ( order ) ) ;
You can’t perform that action at this time.
0 commit comments