Start learning free

LESSON 71 OF 285 · STRINGS I

Top K Brands V1 in JavaScript: Building a Two-Line Result

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 = "Laptop";
const brand = "TechPro";
const rating = 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(...).

JavaScript 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 = "Laptop";
const brand = "TechPro";
const rating = 4;

const productLine = product;
const brandLine = "1. " + brand + " (" + rating + ")";
console.log(productLine + "\n" + brandLine);

STEP 1 OF 3
    Ctrl + Enter to check

    JAVASCRIPT · STRINGS I

    Top K Brands V1 in JavaScript, explained

    Problem overview

    Top K Brands is a small project that grows over several versions. It models a shop's search page: the shopper searches for a product and sees the best-rated brands that sell it. Version 1 has a single product and a single brand, so there is nothing to rank yet. The task is only to format the output.

    The starter code defines product, brand and rating. The program prints two lines: the product name, then a ranked entry in the form 1. Brand (rating).

    Laptop
    1. TechPro (4)
    

    Approach

    The lesson asks you to keep each output line in its own variable, productLine and brandLine, and print them together at the end. Building lines separately is a habit worth keeping.

    The ranked entry is made of several pieces: the rank 1. , the brand, a space and an opening parenthesis, the rating and a closing parenthesis. Joining them with + in the order you read them gives the line.

    Because the product line is just the product, the first variable needs no calculation. The only real work is the second line, where several pieces of text and one number become a single string.

    Step by step

    1. Create productLine and set it to product. The first line is just the product name, so no formatting is needed.
    2. Create brandLine by joining the pieces with +. The rating is a number, but when + has a string on one side, JavaScript converts the number to text and appends it.
    3. Print both lines in one call. Join productLine and brandLine with "\n", the newline character, which starts a new line in the output.

    The solution in JavaScript

    const product = "Laptop";
    const brand = "TechPro";
    const rating = 4;
    
    const productLine = product;
    const brandLine = "1. " + brand + " (" + rating + ")";
    console.log(productLine + "\n" + brandLine);
    

    Points worth noting:

    • "\n" is an escape sequence: the two characters in the source become one newline character in the string. Printing a string that contains it shows two lines, even though console.log was called once.
    • The pieces of brandLine are evaluated from left to right. Each + has a string on its left, so each one produces a longer string. The number rating is converted when it is reached.
    • Spaces belong to the string pieces. " (" carries the space before the parenthesis, and nothing else adds one.
    • The quotes are part of the source code only. console.log prints the text without them.

    Edge cases and common mistakes

    • Passing the pieces as separate arguments. console.log(productLine, brandLine) prints both on one line, separated by a space. Only the "\n" join puts them on two lines.
    • Missing spaces or punctuation. "1." + brand prints 1.TechPro. The rank string needs a space after the period.
    • Starting a number sum by accident. 1 + 2 + "x" gives "3x", because the first + adds two numbers. Starting each line with a string, such as "1. ", avoids that.
    • Forgetting the closing parenthesis after the rating.

    Testing

    • With product = "Phone", brand = "Zenith" and rating = 5, the output is Phone followed by 1. Zenith (5) on the next line.
    • With rating = 0, the second line ends with (0).
    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free