View:
Level:
Original 19 lines
Modified 22 lines
Diff Result
Added Removed Unchanged
Original
Modified
1 function calculateTotal(items) {
1 function calculateTotal(items, discount = 0) {
2 let total = 0;
2 let total = 0;
3 for (let i = 0; i < items.length; i++) {
4 total += items[i].price;
3 for (const item of items) {
4 total += item.price * item.quantity;
5 }
5 }
6 return total;
6 return total * (1 - discount);
7 }
7 }
8
8
9 const TAX_RATE = 0.05;
9 const TAX_RATE = 0.08;
10 const FREE_SHIPPING_THRESHOLD = 500;
10
11
11 function getOrderSummary(order) {
12 function getOrderSummary(order) {
12 const subtotal = calculateTotal(order.items);
13 const subtotal = calculateTotal(order.items, order.discount);
13 const tax = subtotal * TAX_RATE;
14 const tax = subtotal * TAX_RATE;
15 const shipping = subtotal >= FREE_SHIPPING_THRESHOLD ? 0 : 50;
14 return {
16 return {
15 subtotal,
17 subtotal,
16 tax,
18 tax,
17 total: subtotal + tax
19 shipping,
20 total: subtotal + tax + shipping
18 };
21 };
19 }
22 }