Start learning free

LESSON 103 OF 671 · CONDITIONAL STATEMENTS - CHALLENGES

Top K Brands V2 in JavaScript: Picking the Better of Two

Practice Read the full explanation

Two brands sell the same product. Show the one with the higher rating.

You start with:

const brand1 = "TechPro";
const rating1 = 4;
const brand2 = "MegaCorp";
const rating2 = 5;

let bestBrand;
let bestRating;

Build:

1. MegaCorp (5)

Rules:

  • Use if/else to pick the winner: brand1 when rating1 > rating2, otherwise brand2.
  • The starter already declares bestBrand and bestRating: assign the winner's name and rating to them inside the branches.
  • Format the result as "1. Brand (rating)" and print it with console.log(...).

Pick the winner into bestBrand and bestRating first, then build the output text from them.

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

const brand1 = "TechPro";
const rating1 = 4;
const brand2 = "MegaCorp";
const rating2 = 5;

let bestBrand;
let bestRating;

if (rating1 > rating2) {
    bestBrand = brand1;
    bestRating = rating1;
} else {
    bestBrand = brand2;
    bestRating = rating2;
}
console.log("1. " + bestBrand + " (" + bestRating + ")");

STEP 1 OF 5
    Ctrl + Enter to check

    JAVASCRIPT · CONDITIONAL STATEMENTS - CHALLENGES

    Top K Brands V2 in JavaScript, explained

    Problem overview

    Version 1 printed one brand. Version 2 has two brands that sell the same product: brand1 with rating1 and brand2 with rating2. The program prints only the better-rated one, in the ranked form 1. Brand (rating).

    In the starter data TechPro has rating 4 and MegaCorp has rating 5, so the output is:

    1. MegaCorp (5)
    

    The starter code also declares bestBrand and bestRating without values. The branches of an if/else fill them in.

    Approach

    Compare the two ratings. If rating1 is strictly greater than rating2, the first brand wins. In every other case, the second brand wins, including a tie. The rules for this version say the else branch takes the second brand, so on equal ratings the output shows brand2.

    The winning brand's name and rating travel together. Each branch sets both variables, and the line is printed once, after the if/else, from whichever pair was stored. This avoids two separate console.log calls that would each need the formatting.

    Step by step

    1. Write the if with the condition rating1 > rating2, and inside it set bestBrand to brand1.
    2. Set bestRating to rating1 in the same branch, so the stored name and rating belong to the same brand.
    3. Add the else and set bestBrand to brand2 in it. The else needs no condition: it runs whenever rating1 > rating2 is false.
    4. Set bestRating to rating2 inside the else.
    5. Print the result after the if/else: "1. ", the best brand, " (", the best rating and ")", joined with +.

    The lesson has you write the else branch with its closing brace first and then fill it in. Doing the same for the if keeps every pair of braces balanced while you type.

    The solution in JavaScript

    const brand1 = "TechPro";
    const rating1 = 4;
    const brand2 = "MegaCorp";
    const rating2 = 5;
    
    let bestBrand;
    let bestRating;
    
    if (rating1 > rating2) {
        bestBrand = brand1;
        bestRating = rating1;
    } else {
        bestBrand = brand2;
        bestRating = rating2;
    }
    console.log("1. " + bestBrand + " (" + bestRating + ")");
    

    Language details to notice:

    • let bestBrand; declares a variable with no value. Until it is assigned, it holds undefined. They are let because const needs its value at the declaration and can't be assigned later.
    • The variables are declared above the if, in the enclosing scope, so the console.log after the block can still read them. A let declared inside a branch would be gone at the closing brace.
    • In "1. " + bestBrand + " (" + bestRating + ")", every + has a string on its left, so the number bestRating is converted to text when it is joined.
    • Exactly one branch runs, so both variables always get a value before the print.

    Why keep the winner in variables

    There are two other ways to write this, and both are worse for a project that keeps growing. The first is to put a console.log inside each branch. Each branch would then repeat the formatting code, and a change to the output format would have to be made twice. The second is to store only the winning brand and look up its rating afterwards. Since the brands are in separate variables, finding that rating would need a second comparison.

    Storing both values in bestBrand and bestRating separates two jobs: deciding who wins, which is the if/else, and showing the winner, which is the last line. Each job can change without touching the other.

    JavaScript also has a conditional operator, written condition ? a : b, which can choose one value in a single expression. It suits a choice between two values. The lesson uses a full if/else because two values must be set together. Learn the block form first, and the shorter form is easy to read afterwards.

    The ratings in the starter data differ, but the code also has to behave when they don't. For equal ratings of 3 and 3 the condition 3 > 3 is false, so the else branch runs and the output is 1. MegaCorp (3). This is worth knowing before you test, because it is the one case where the choice between > and >= shows up in the output.

    Tracing the starter data

    The condition is 4 > 5, which is false. The program skips the if body and runs the else body, so bestBrand becomes "MegaCorp" and bestRating becomes 5. The last line builds 1. MegaCorp (5). If the ratings were reversed, with TechPro at 5 and MegaCorp at 4, the condition would be true and the output would be 1. TechPro (5).

    Edge cases and common mistakes

    • Ties. With equal ratings, rating1 > rating2 is false, so brand2 is printed. Changing the condition to >= would give the tie to brand1 instead, which is a different behaviour.
    • Updating only one variable. If the if sets bestBrand but forgets bestRating, the printed line shows (undefined) for that case.
    • Declaring the variables inside the branches. The final console.log would fail with bestBrand is not defined.
    • Using = in the condition. if (rating1 = rating2) assigns instead of comparing.

    Testing

    • Ratings 4 and 5 print 1. MegaCorp (5).
    • Ratings 5 and 4 print 1. TechPro (5).
    • Ratings 3 and 3 print 1. MegaCorp (3).
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free