diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js index b3a36f4e4..e50c07b0f 100644 --- a/Sprint-1/destructuring/exercise-3/exercise.js +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -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)}`); +