Practice Problem: Shopping Cart Total Calculator

Write your JavaScript in the browser console. The page only shows the task.

Your Task
  • Create a function named calculateTotal.
  • Use a for loop to sum price × quantity for each product in cart.
  • If the total is greater than 5000, compute a 10% discount.
  • Log to the console:
    • Total before discount
    • Discount applied (0 if no discount)
    • Final total (after discount)

Sample Data (copy for your console)

let cart = [
  { name: "Laptop",  price: 4000, quantity: 1 },
  { name: "Mouse",   price:  500, quantity: 2 },
  { name: "Keyboard",price: 1500, quantity: 1 }
];

let total = 0;
let discount = 0;

You can change prices/quantities to test different cases.

Console Checklist

  • Declare/initialize your total before the loop.
  • Loop index (e.g., i) must exist and be used consistently.
  • Do not return from inside the loop if you still need to sum more items.
  • Calculate any discount before you finish/return.

Open DevTools → Console (Windows: Ctrl+Shift+J, macOS: Cmd+Option+J).