Use an if/else to decide whether to charge shipping based on a free-shipping threshold.
You start with:
subtotal := 75
shippingCost := 10
freeShippingThreshold := 100
Build:
Subtotal: $75 Shipping: $10 Total: $85
Rules:
- If
subtotal >= freeShippingThreshold, setshippingto0; otherwise set it toshippingCost. total := subtotal + shipping.- Always include all three lines (
Subtotal,Shipping,Total). When shipping is free, the line shows$0: for a subtotal of 120 the lines would beSubtotal: $120,Shipping: $0,Total: $120. - Join the three lines with a newline
"\n"into one stringresult, then print it withfmt.Println(result).
Decide
shipping in an if/else first, then build the three lines.
Declare
var shipping int before the if, so it is still visible after the if/else, then assign it in both branches.
Use
strconv.Itoa(...) to convert each number to text before concatenating it.
subtotal := 75
shippingCost := 10
freeShippingThreshold := 100
var shipping int
if subtotal >= freeShippingThreshold {
shipping = 0
} else {
shipping = shippingCost
}
total := subtotal + shipping
line1 := "Subtotal: $" + strconv.Itoa(subtotal)
line2 := "Shipping: $" + strconv.Itoa(shipping)
line3 := "Total: $" + strconv.Itoa(total)
result := line1 + "\n" + line2 + "\n" + line3
fmt.Println(result)
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
