Skip to content

Commit f52238d

Browse files
committed
use descructuring in the paramter of forEach, and make string padding more dynamic.
Don't have to edit the string, just update the padding variables.
1 parent dcb2eaa commit f52238d

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ let order = [
88
];
99

1010
function printReceipt(order) {
11+
const qtyWidth = 8;
12+
const itemWidth = 20;
13+
1114
console.log(`${"QTY".padEnd(8)}${"ITEM".padEnd(20)}TOTAL`);
1215
let total = 0;
13-
order.forEach((item) => {
14-
const { quantity, itemName, unitPricePence } = item;
16+
order.forEach(({ quantity, itemName, unitPricePence }) => {
1517
console.log(
16-
`${String(quantity).padEnd(8)}${String(itemName).padEnd(20)}${((quantity * unitPricePence) / 100).toFixed(2)}`
18+
`${String(quantity).padEnd(qtyWidth)}${String(itemName).padEnd(itemWidth)}${((quantity * unitPricePence) / 100).toFixed(2)}`
1719
);
1820
total += quantity * unitPricePence;
1921
});

0 commit comments

Comments
 (0)