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
- Create
productLineand set it toproduct. The first line is just the product name, so no formatting is needed. - Create
brandLineby 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. - Print both lines in one call. Join
productLineandbrandLinewith"\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 thoughconsole.logwas called once.- The pieces of
brandLineare evaluated from left to right. Each+has a string on its left, so each one produces a longer string. The numberratingis 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.logprints 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." + brandprints1.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"andrating = 5, the output isPhonefollowed by1. Zenith (5)on the next line. - With
rating = 0, the second line ends with(0).
