Skip to content

Commit 6a1789a

Browse files
committed
feat: implement getReceipt function to generate order receipt
1 parent e58b013 commit 6a1789a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff 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));

0 commit comments

Comments
 (0)