Skip to content

Commit 58566d9

Browse files
committed
move receipt string building to a helper function
1 parent f52238d commit 58566d9

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,28 @@ let order = [
77
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
88
];
99

10-
function printReceipt(order) {
10+
// helper function that pads each item in a line by a certain amount
11+
function getReceiptString(quantity, itemName, total) {
1112
const qtyWidth = 8;
1213
const itemWidth = 20;
1314

14-
console.log(`${"QTY".padEnd(8)}${"ITEM".padEnd(20)}TOTAL`);
15+
return `${String(quantity).padEnd(qtyWidth)}${itemName.padEnd(itemWidth)}${total}`;
16+
}
17+
18+
function printReceipt(order) {
19+
// print header
20+
console.log(getReceiptString("QTY", "ITEM", "TOTAL"));
21+
1522
let total = 0;
23+
24+
// print each line item
1625
order.forEach(({ quantity, itemName, unitPricePence }) => {
17-
console.log(
18-
`${String(quantity).padEnd(qtyWidth)}${String(itemName).padEnd(itemWidth)}${((quantity * unitPricePence) / 100).toFixed(2)}`
19-
);
26+
const price = ((quantity * unitPricePence) / 100).toFixed(2);
27+
console.log(getReceiptString(quantity, itemName, price));
2028
total += quantity * unitPricePence;
2129
});
30+
31+
// print total
2232
console.log(`\nTotal: ${(total / 100).toFixed(2)}`);
2333
}
2434

0 commit comments

Comments
 (0)