Your question is Find Bug in Swift Snippet. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Rakuten's shopping app checkout screen applies a loyalty discount and lets shoppers redeem Rakuten Points before totalling the order. A mobile engineer on the checkout team wrote the function below, and QA is reporting totals that do not match what the receipt should show.
struct CartItem {
let sku: String
let priceYen: Int
let quantity: Int
}
func checkoutTotal(items: [CartItem], pointsAvailable: Int, prices: [String: Int]) -> Int {
var subtotal = 0
for item in items {
let unitPrice = prices[item.sku]!
subtotal += unitPrice * item.quantity
}
let discountPercent = 10
let discountedTotal = subtotal - (subtotal * discountPercent / 100)
var pointsUsed = pointsAvailable
if pointsUsed > discountedTotal {
pointsUsed = discountedTotal
}
var finalTotal = 0
for item in items {
finalTotal += item.priceYen * item.quantity
}
finalTotal -= pointsUsed
let shippingFee = subtotal > 3000 ? 0 : 500
return finalTotal + shippingFee
}
Find the bugs in this Swift snippet and explain what each one causes at checkout.