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
- Write the
if. The condition israting1 > rating2. Inside it, setbestBrandtobrand1. The starter code declared the variable without a value, and the branches give it one. - Set
bestRatingtorating1, still inside theif, so the winner's name and rating stay together. - Add the
else. It runs when the first brand is not strictly better, which includes a tie. SetbestBrandtobrand2. - Set
bestRatingtorating2inside theelse. - Print the result with a single
console.log:"1. ",bestBrand," (",bestRatingand")", 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:
letwithout a value.let bestBrand: string;creates the variable now and lets a later statement assign it. Aconstcan'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 theif/elsecan read them. - Types are erased.
stringandnumberare 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 becomes1. TechPro (5). - Setting only the brand. If the
bestRatingassignment is left out of theelse, the program runs and prints1. MegaCorp (undefined), because an unassigned variable holdsundefined. - The first brand is higher. With ratings 5 and 2 the
ifbranch runs and the output is1. 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).
