Kodewick
Start learning →
Mini challenge←→0 / 70

Stationery bill

#055Mini challengeStructsSlices6 min

Each line on the bill is a Line: a name, a unit Price and a quantity Qty. First write the method Cost() on Line to return that line's cost (price times quantity); then write Receipt(lines) to add up the whole bill with it. An empty bill gives 0.

Examples
Receipt([]Line{{"pencil", 2, 5}, {"notebook", 8, 1}})→18
Receipt([]Line{})→0
The functionfunc Receipt(lines []Line) int
main.go
package main

type Line struct {
	Name  string
	Price int
	Qty   int
}

func (l Line) Cost() int {
	// your code here: return price times quantity
	return 0
}

func Receipt(lines []Line) int {
	total := 0
	for _, l := range lines {
		// your code here: add this line's Cost() to total
	}
	return total
}
Ctrl + Enter runs too
Result

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