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
- Write the
ifwith the conditionrating1 > rating2, and inside it setbestBrandtobrand1. - Set
bestRatingtorating1in the same branch, so the stored name and rating belong to the same brand. - Add the
elseand setbestBrandtobrand2in it. Theelseneeds no condition: it runs wheneverrating1 > rating2is false. - Set
bestRatingtorating2inside theelse. - 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 holdsundefined. They areletbecauseconstneeds its value at the declaration and can't be assigned later.- The variables are declared above the
if, in the enclosing scope, so theconsole.logafter the block can still read them. Aletdeclared inside a branch would be gone at the closing brace. - In
"1. " + bestBrand + " (" + bestRating + ")", every+has a string on its left, so the numberbestRatingis 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 > rating2is false, sobrand2is printed. Changing the condition to>=would give the tie tobrand1instead, which is a different behaviour. - Updating only one variable. If the
ifsetsbestBrandbut forgetsbestRating, the printed line shows(undefined)for that case. - Declaring the variables inside the branches. The final
console.logwould fail withbestBrand 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).
