Start learning free

LESSON 73 OF 289 · STRINGS I

Top K Brands V1 in TypeScript: One Brand

Practice Read the full explanation

E-commerce search page: when a shopper searches a product, show the top-rated brand selling it. In v1 there's just one product and one brand.

You start with:

const product: string = "Laptop";
const brand: string = "TechPro";
const rating: number = 4;

Build:

Laptop
1. TechPro (4)

Rules:

  • The first line is the product name.
  • The second line is a ranked entry written as "1. Brand (rating)".
  • Keep each line in its own variable (productLine and brandLine), join them with a newline "\n" and print the result with console.log(...).

TypeScript turns the number into text for you when you join it to a string with +.

productLine + "\n" + brandLine joins both lines into one string.

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);

STEP 1 OF 3
    Ctrl + Enter to check

    TYPESCRIPT · STRINGS I

    Top K Brands V1 in TypeScript, explained

    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

    1. Create productLine. It is a const typed as string and set to product. The first output line is just the product name, so no extra text is needed.
    2. Create brandLine. Concatenate "1. ", brand, " (", rating and ")". The rating is a number, and + converts it to text because the other operand is a string.
    3. Print both lines. Join productLine and brandLine with "\n" inside a single console.log call. 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 text 4 without any conversion call.
    • Types are erased. The : string and : number annotations 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.log prints 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 gives 1.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 give 5. . Starting with a string avoids this.
    • Typing /n instead 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 = 4 prints Laptop then 1. TechPro (4).
    • product = "Phone", brand = "Zenith", rating = 5 prints Phone then 1. Zenith (5).
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free