Kodewick
Start learning →
Basic←→0 / 70

Average wait

#059BasicErrorsSlices4 min

The takeaway shop notes how many minutes each delivery took. Write AvgWait(minutes) to return the average wait (whole minutes, integer division) and nil. With no deliveries there is no average, so return 0 and the error "no deliveries".

Examples
AvgWait([]int{20, 30, 40})→(30, None)
AvgWait([]int{})→(0, 'no deliveries')
The functionfunc AvgWait(minutes []int) (int, error)
main.go
package main

// your code here: import "errors"

func AvgWait(minutes []int) (int, error) {
	// your code here: no deliveries? return 0 and the error "no deliveries"
	total := 0
	for _, m := range minutes {
		total += m
	}
	return total / len(minutes), nil
}
Ctrl + Enter runs too
Result

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