Skip to content

Commit 7e6fbb9

Browse files
Refactor order mapping to use forEach and improve price calculation display
1 parent b5b2a8e commit 7e6fbb9

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

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

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

10-
order.map(({ itemName, quantity, unitPricePence }) => {
10+
order.forEach(({ itemName, quantity, unitPricePence }) => {
1111
let totalPence = quantity * unitPricePence;
1212
let pence = totalPence % 100;
1313
let paddedPence = String(pence).padStart(2, "0");
14-
let pounds = String(totalPence).substring(0, String(paddedPence).length - 1);
15-
let priceInPoundsAndPence=`${pounds}.${paddedPence}`
16-
console.log(pence,priceInPoundsAndPence);
17-
18-
19-
14+
let pounds = Math.floor(totalPence / 100);
15+
let priceEachItem = `${pounds}.${paddedPence}`;
16+
17+
console.log(
18+
`${String(quantity).padEnd(7, " ")}${itemName.padEnd(
19+
20,
20+
" "
21+
)}${priceEachItem}`
22+
);
23+
});
24+
let sumAllPence = 0;
25+
order.forEach(({ quantity, unitPricePence }) => {
26+
sumAllPence += quantity * unitPricePence;
2027
});
28+
let totalBillPence = sumAllPence % 100;
29+
let paddedTotalBillPence = String(totalBillPence).padStart(2, "0");
30+
let totalBillPounds = Math.floor(sumAllPence / 100);
31+
let totalBill = `
32+
Total: ${totalBillPounds}.${totalBillPence}`;
33+
34+
console.log(totalBill);

0 commit comments

Comments
 (0)