Start learning free

LESSON 105 OF 289 · CONDITIONAL STATEMENTS - CHALLENGES

Top K Brands V2 in TypeScript: Better of Two with if/else

Practice Read the full explanation

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

You start with:

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

let bestBrand: string;
let bestRating: number;

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.

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

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

let bestBrand: string;
let bestRating: number;

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

    TYPESCRIPT · CONDITIONAL STATEMENTS - CHALLENGES

    Top K Brands V2 in TypeScript, explained

    Problem overview

    Two brands sell the same product, and the program must show the one with the higher rating. The starter code has four constants, brand1, rating1, brand2 and rating2, and two variables declared without values, bestBrand and bestRating. The program fills those two in and prints one ranked line in the form 1. Brand (rating).

    TechPro has a rating of 4 and MegaCorp has 5, so MegaCorp wins:

    1. MegaCorp (5)

    Approach

    This is a choice between two cases, so an if/else fits. The condition is rating1 > rating2. When it holds, the first brand wins and its name and rating go into bestBrand and bestRating. Otherwise the second brand wins and its values go there instead. After the if/else, one console.log prints the result, and it doesn't need to know which branch ran.

    The name and the rating always travel together: whichever branch runs must set both variables, or the printed line mixes data from different brands.

    The comparison is strict, which decides ties. When the ratings are equal, rating1 > rating2 is false, and the else branch picks the second brand. With both ratings at 3, the output is 1. MegaCorp (3).

    Tracing the example: rating1 > rating2 compares 4 with 5, which is false, so the else branch runs. It sets bestBrand to "MegaCorp" and bestRating to 5, and the last line prints them.

    An else with no condition is enough here because there are only two brands. If the first one doesn't win outright, the second one has to be chosen, so there is nothing left to test. The same decision could be written as two separate if statements, but then both conditions would need to be exact opposites, and a tie would be easy to drop by accident. The single if/else covers every case with one comparison.

    Another point is what happens to the two blank variables. They are declared at the top, before the condition, and each branch gives them values. Whatever the ratings are, exactly one branch runs, so by the time of the console.log both variables have been assigned.

    The result line is built from the two winner variables, not from the original constants. That is the benefit of the pattern: the printing code is written once, and it works for whichever brand won. If the data changes so that TechPro has a rating of 5 against MegaCorp's 2, the same line prints 1. TechPro (5) with no change to the code.

    Step by step

    1. Write the if. The condition is rating1 > rating2. Inside it, set bestBrand to brand1. The starter code declared the variable without a value, and the branches give it one.
    2. Set bestRating to rating1, still inside the if, so the winner's name and rating stay together.
    3. Add the else. It runs when the first brand is not strictly better, which includes a tie. Set bestBrand to brand2.
    4. Set bestRating to rating2 inside the else.
    5. Print the result with a single console.log: "1. ", bestBrand, " (", bestRating and ")", joined with +. Because the expression starts with a string, the number is converted to text.

    The solution in TypeScript

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

    A few details are worth knowing:

    • let without a value. let bestBrand: string; creates the variable now and lets a later statement assign it. A const can't do that, since it must be initialised where it is declared.
    • Assignment in branches. Both variables are declared outside the if, so the line after the if/else can read them.
    • Types are erased. string and number are checked by the compiler and vanish from the JavaScript that runs. Because the lesson compiles with strict checks off, the compiler doesn't insist that every path assigns the variables.

    Complexity

    The program makes one comparison and a fixed number of assignments, so it runs in constant time, O(1), and uses constant space for its two variables.

    Edge cases and common mistakes

    • Equal ratings. With both ratings at 3, the program prints 1. MegaCorp (3), the second brand. Changing the condition to >= would give the tie to the first brand: with both ratings at 5 the output becomes 1. TechPro (5).
    • Setting only the brand. If the bestRating assignment is left out of the else, the program runs and prints 1. MegaCorp (undefined), because an unassigned variable holds undefined.
    • The first brand is higher. With ratings 5 and 2 the if branch runs and the output is 1. TechPro (5).
    • Putting the rating first. The expected form is rank, brand, then the rating in parentheses. Check the spaces: one after 1. and one before the parenthesis.

    Testing

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