Practice variables, arithmetic, and string concatenation by printing a tiny one-item receipt.
You start with:
let item = "Coffee Mug"
let price = 12
let quantity = 3
Build:
Coffee Mug x3 Total: $36
Rules:
- Compute
total = price * quantity. - First line:
"<item> x<quantity>"(e.g."Coffee Mug x3"). - Second line:
"Total: $<total>". - Join the two lines with a newline
"\n"into one stringresult, then print it withprint(result).
Swift's string interpolation
\(...) embeds any value directly in a string literal.
line1 + "\n" + line2 joins both lines into one string.
let item = "Coffee Mug"
let price = 12
let quantity = 3
let total = price * quantity
let line1 = "\(item) x\(quantity)"
let line2 = "Total: $\(total)"
let result = line1 + "\n" + line2
print(result)
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
