Problem overview
This is the first version of a small search-page project: a shopper searches a product and the page shows the top-rated brand that sells it. In version 1 there is one product and one brand, so nothing needs ranking yet. The program only formats them.
The starter code has three constants: product ("Laptop"), brand ("TechPro") and rating (4). The output is two lines: the product name, then a ranked entry written as 1. Brand (rating):
Laptop
1. TechPro (4)
The rules ask for each line to live in its own variable, productLine and brandLine, and for the program to join them with a newline and print the result once.
Approach: build each line, then join
The output has two lines with different shapes, so the program builds them separately. The first line is the product name as it is. The second mixes fixed text (1. , a space, parentheses) with two values, a string and an integer.
Swift's string interpolation is made for this. Putting \(brand) inside a string literal inserts the value of brand at that spot, and \(rating) does the same for the number, converting it to text automatically. The whole second line becomes a single literal with two holes in it.
Last, the two lines are joined into one string with a newline character, "\n", between them. A newline in a string ends the current line when it is printed, so one print call produces two lines.
Step by step
- Create
productLineas aletequal toproduct. The first output line is just the product name. - Create
brandLineas aletholding the ranked entry: the rank1., the brand, a space and an opening parenthesis, the rating, then a closing parenthesis. Use interpolation for the two values. - Print the two lines in one call:
productLine, then"\n", thenbrandLine, joined with+.
The solution in Swift
let product = "Laptop"
let brand = "TechPro"
let rating = 4
let productLine = product
let brandLine = "1. \(brand) (\(rating))"
print(productLine + "\n" + brandLine)
The Swift details worth knowing:
- Interpolation handles the type.
ratingis anInt, yet"\(rating)"is fine. Any expression can go inside the parentheses, for example"\(rating + 1)", which would insert 5 here. +only joins strings with strings. A version that builds the line with"1. " + brand + " (" + rating + ")"does not compile: the compiler reportscannot convert value of type 'Int' to expected argument type 'String'. Interpolation avoids the problem.String(rating)is the other fix."\n"is one character. The backslash starts an escape sequence, so the two characters in the source become a single newline character in the string. This is different from the\(in interpolation, which also starts with a backslash but means "insert a value".- Constants. Every name here is a
let: each value is set once and never changes. - Alternatives.
print(productLine)followed byprint(brandLine)would print the same two lines, becauseprintends its output with a newline. The lesson joins them with"\n"so that you practise building a multi-line string.
Edge cases and common mistakes
- Spacing and punctuation. The entry must read exactly
1. TechPro (4): a period after the rank, one space after it, one space before the opening parenthesis. A missing space or a stray one changes the text. - Joining a number with
+. This is the most common error here, as shown above. - Forgetting the newline.
productLine + brandLineprintsLaptop1. TechPro (4)on one line, with the two pieces glued together. - Using
"/n". A forward slash is just a character, and the text would appear literally. The newline escape uses a backslash. - Hard-coding the values. Writing
"1. TechPro (4)"as a literal passes today's data but ignoresbrandandrating. Use the variables, so the output follows the data.
Testing
- The starter data prints
Laptopand then1. TechPro (4).
