Start learning free

LESSON 92 OF 404 · CONDITIONAL STATEMENTS - CHALLENGES

Top K Brands V2 - Better of Two in Go

Practice

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

You start with:

brand1 := "TechPro"
rating1 := 4
brand2 := "MegaCorp"
rating2 := 5

var bestBrand string
var bestRating int

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 fmt.Println(...).

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

Go needs strconv.Itoa(...) to turn an int into text before joining it with +.

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) + ")")

STEP 1 OF 5
    Ctrl + Enter to check

    Top K Brands V2 in Go

    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free