Kodewick
Start learning →
Mini challenge←→0 / 70

Hand-typed scores

#060Mini challengeErrorsLoops6 min

The scores on the board were typed in by hand. Write TotalScores(texts): an empty string "" is someone who didn't come, so skip it; if one isn't a number, return 0 and the error strconv.Atoi gave, straight away; otherwise return the total and nil.

Examples
TotalScores([]string{"8", "", "7"})→(15, None)
TotalScores([]string{"9", "ten"})→(0, 'strconv.Atoi: parsing "ten": invalid syntax')
The functionfunc TotalScores(texts []string) (int, error)
main.go
package main

import "strconv"

func TotalScores(texts []string) (int, error) {
	total := 0
	for _, t := range texts {
		// your code here: skip t if it's an empty string
		n, err := strconv.Atoi(t)
		if err != nil {
			// your code here: return 0 and err
		}
		total += n
	}
	return total, nil
}
Ctrl + Enter runs too
Result

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