Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Sprint-1/destructuring/exercise-3/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,30 @@ let order = [
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
];

// Column widths (matching the expected output)
const QTY_WIDTH = 8;
const ITEM_WIDTH = 20;

// Print header
console.log(`${"QTY".padEnd(QTY_WIDTH)}${"ITEM".padEnd(ITEM_WIDTH)}TOTAL`);

let totalOrder = 0;

// Process each item using object destructuring
for (const { itemName, quantity, unitPricePence } of order) {
const itemTotalPence = quantity * unitPricePence;
const itemTotalPounds = (itemTotalPence / 100).toFixed(2);

// Print the line item
console.log(
`${quantity.toString().padEnd(QTY_WIDTH)}${itemName.padEnd(ITEM_WIDTH)}${itemTotalPounds}`
);

// Accumulate total (parseFloat to convert string back to number)
totalOrder += parseFloat(itemTotalPounds);
}

// Print final total (fixed to 2 decimal places)
console.log(`\nTotal: ${totalOrder.toFixed(2)}`);

Loading