@@ -6,3 +6,54 @@ let order = [
66 { itemName : "Hot Coffee" , quantity : 2 , unitPricePence : 100 } ,
77 { itemName : "Hash Brown" , quantity : 4 , unitPricePence : 40 } ,
88] ;
9+
10+ // Print the header for the receipt
11+
12+ console . log ( "QTY ITEM TOTAL" ) ;
13+
14+ // Function to print the order details
15+ // Use destructuring to access itemName, quantity, and unitPricePence
16+
17+ function printOrder ( itemName , quantity , unitPricePence ) {
18+
19+ // Iterate over each item in the order
20+ // Use forEach to loop through the order array
21+ // Use destructuring to access itemName, quantity, and unitPricePence
22+ // Calculate total price for each item
23+
24+ order . forEach ( ( { itemName, quantity, unitPricePence } ) => {
25+ const totalPrice = ( quantity * unitPricePence ) / 100 ;
26+ console . log (
27+ `${ quantity . toString ( ) . padStart ( 2 ) } ${ itemName . padEnd ( 20 ) } £${ totalPrice . toFixed ( 2 ) } `
28+ ) ;
29+ } ) ;
30+
31+ // Calculate total cost of the order
32+ // Use destructuring to access quantity and unitPricePence
33+ // Use reduce to sum up the total cost
34+ // Format the total cost to two decimal places and prefix with a currency symbol
35+ // Log the total cost to the console
36+ // Use toFixed(2) to ensure two decimal places
37+ // Use console.log to print the total cost
38+
39+ const totalCost = order . reduce ( ( acc , { quantity, unitPricePence } ) => {
40+ return acc + ( quantity * unitPricePence ) ;
41+ } , 0 ) / 100 ;
42+ console . log ( `\nTotal: £${ totalCost . toFixed ( 2 ) } ` ) ;
43+ }
44+ // Call the function to print the order
45+ printOrder ( order ) ;
46+
47+
48+
49+
50+ // Expected result
51+
52+ // QTY ITEM TOTAL
53+ // 1 Hot cakes £2.32
54+ // 2 Apple Pie £2.78
55+ // 1 Egg McMuffin £2.80
56+ // 1 Sausage McMuffin £3.00
57+ // 2 Hot Coffee £2.00
58+ // 4 Hash Brown £1.60
59+ // Total: £14.50
0 commit comments