Kodewick
Start learning →
Basic←→0 / 70

Cheapest drink

#051BasicStructsSlices4 min

The drinks menu is a []Drink (at least one), each with a Name and a Price. Write Cheapest(menu) to return the cheapest one's name; if two cost the same, keep the earlier one.

Examples
Cheapest([]Drink{{"latte", 18}, {"tea", 12}, {"mocha", 22}})→'tea'
Cheapest([]Drink{{"water", 3}})→'water'
The functionfunc Cheapest(menu []Drink) string
main.go
package main

type Drink struct {
	Name  string
	Price int
}

func Cheapest(menu []Drink) string {
	best := menu[0]
	for _, d := range menu {
		// your code here: if d is cheaper than best, take d
	}
	return best.Name
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.