Problem overview
This version has two brands selling the same product: brand1 with rating1, and brand2 with rating2. The program prints the one with the higher rating as a ranked entry, 1. Brand (rating). With the starting data, TechPro has 4 and MegaCorp has 5, so the output is:
1. MegaCorp (5)
The starter already declares two empty variables, bestBrand and bestRating. The lesson fills them in from the winner's data, then prints once.
Approach: choose, then format once
The winner is picked by comparing the two ratings. The formatting of the entry is the same whichever brand wins, so it should not be written twice. The pattern is to let the if/else copy the winner's name and rating into bestBrand and bestRating, and to build the output line after the branches from those two variables.
The rule is that brand1 wins only when rating1 > rating2. On a tie, the else branch runs and brand2 is chosen. This is a strict comparison, and the tie behaviour is part of the lesson's contract.
Traced on the starting data: 4 > 5 is false, so the else branch runs. bestBrand becomes "MegaCorp" and bestRating becomes 5, and the last line prints 1. MegaCorp (5). If the ratings were swapped, the if branch would run and TechPro would be printed with its own rating.
Why keep both the name and the rating, when the rating alone decides the winner? Because the output needs both, and once the branches have run, the program no longer knows which brand won unless the variables remember it. Storing the pair is the simplest answer for two brands.
Before the if, bestBrand and bestRating hold the zero values. Printing them at that point would give an empty brand and a rating of 0, which is a quick way to see that the branches really do the filling.
Because the starter already declares both variables, the lesson's own code only assigns to them. That is the reason the branches use = and not :=. The operator := would declare a second, new variable inside the branch, and the outer one would never change. When you write this pattern yourself in another program, the declaration has to come before the if, in the scope that the later code can see.
Step by step
- Write the
if. The condition israting1 > rating2. Inside it, assignbrand1tobestBrand. - Assign the rating in the same branch:
bestRating = rating1. The brand and its rating always travel together. - Add the
else, on the same line as the closing brace of theif, and assignbrand2tobestBrandin it. - Assign the second rating:
bestRating = rating2inside theelse, then close the brace. - Print the entry. Join
"1. ",bestBrand," (",strconv.Itoa(bestRating)and")"with+and pass the string tofmt.Println.
The solution in Go
brand1 := "TechPro"
rating1 := 4
brand2 := "MegaCorp"
rating2 := 5
var bestBrand string
var bestRating int
if rating1 > rating2 {
bestBrand = brand1
bestRating = rating1
} else {
bestBrand = brand2
bestRating = rating2
}
fmt.Println("1. " + bestBrand + " (" + strconv.Itoa(bestRating) + ")")
The program uses the fmt and strconv packages.
The details worth knowing
- Declared before the branches.
var bestBrand stringcreates a variable holding"", andvar bestRating intcreates one holding0. Both live in the surrounding scope, so thePrintlnafter theifcan read them. If a branch wrotebestBrand := brand1instead ofbestBrand = brand1, it would create a new variable that disappears at the closing brace. The compiler would reportdeclared and not usedfor it, and the outerbestBrandwould stay empty. - The condition must be a
bool.rating1 > rating2is one. There are no parentheses around it, and the braces are required. - Assigning is not using. Go reports a variable as unused when it is only ever assigned and never read. If the final line printed only
bestBrand, the build would stop withbestRating declared and not used, even though both branches assign it. The finalPrintlnreads both variables, so the program compiles. - Text and numbers. The rating is an
int, so it goes throughstrconv.Itoabefore+can join it to strings.
Complexity
There is a single comparison and a few assignments, so time and extra space are O(1).
Edge cases and common mistakes
- A tie. With
rating1 := 5andrating2 := 5the condition is false and the output namesbrand2. Using>=would give the tie tobrand1and fail the lesson's tests. - Copying only the name. If a branch sets
bestBrandbut forgetsbestRating, the printed rating is 0, or stays at whatever value an earlier assignment left. - Printing the wrong rating. Printing
rating1in the last line, instead ofbestRating, shows the first brand's rating next to the second brand's name whenever the second brand wins. - Reversed comparison.
rating1 < rating2as the condition would reverse the choice, so the lower-rated brand would be printed. - Formatting inside both branches. It works, but the entry format is then written twice. Keep it after the
if/else.
Testing
- TechPro 4 and MegaCorp 5 print
1. MegaCorp (5). - TechPro 5 and MegaCorp 4 print
1. TechPro (5). - TechPro 3 and MegaCorp 3 print
1. MegaCorp (3), the second brand on a tie.
