Problem overview
This is the first version of a small search-results program. A shopper searches for a product and the page shows the best-rated brand that sells it. In this version there is one product and one brand, so the task is only about formatting. The data is three constants: product, brand and rating.
The program prints two lines. The first is the product name. The second is a ranked entry in the form 1. Brand (rating):
Laptop
1. TechPro (4)
With a single brand, that brand is automatically first.
Approach
Build each output line as its own string variable, then join the two with a newline and print once. Keeping a line per variable makes each piece easy to read and easy to change, and the final console.log only has to glue the lines together.
The brand line has several parts: the rank 1. , the brand name, a space, an opening parenthesis, the rating, and a closing parenthesis. Joined with +, they read in the same order as the finished text.
Step by step
- Create
productLine. It is aconsttyped asstringand set toproduct. The first output line is just the product name, so no extra text is needed. - Create
brandLine. Concatenate"1. ",brand," (",ratingand")". The rating is anumber, and+converts it to text because the other operand is a string. - Print both lines. Join
productLineandbrandLinewith"\n"inside a singleconsole.logcall. The"\n"escape is the newline character, so the output breaks into two lines at that point.
The solution in TypeScript
const product: string = "Laptop";
const brand: string = "TechPro";
const rating: number = 4;
const productLine: string = product;
const brandLine: string = "1. " + brand + " (" + rating + ")";
console.log(productLine + "\n" + brandLine);
Details that matter here:
- Left-to-right evaluation.
+is evaluated from left to right. Because the expression starts with the string"1. ", every following+concatenates, and the number 4 becomes the text4without any conversion call. - Types are erased. The
: stringand: numberannotations are checked by the compiler and do not exist in the JavaScript it writes. They do not convert anything at run time. - One call, two lines. The newline lives inside the string, so one
console.logprints both lines. Two separate calls would print the same result, but the lesson asks for a single joined string.
Edge cases and common mistakes
- A missing space.
"1."without the trailing space gives1.TechPro. The same goes for the space before the parenthesis, which is part of" (". - Adding numbers instead of joining them. If the expression began with two numbers, such as
1 + rating + ". ", they would be added first and give5.. Starting with a string avoids this. - Typing
/ninstead of\n. The newline escape uses a backslash. A forward slash prints as the two characters/n. - Hard-coding the output. Writing
"1. TechPro (4)"directly passes this one case, but the output should come from the variables, so it follows when their values change.
Testing
product = "Laptop",brand = "TechPro",rating = 4printsLaptopthen1. TechPro (4).product = "Phone",brand = "Zenith",rating = 5printsPhonethen1. Zenith (5).
