Kodewick
Start learning →
Mini challenge←→0 / 70

Alarm clock time

#062Mini challengeErrorsStrings7 min

The alarm clock stores times like "07:30". Write Minutes(clock) to return the minutes since midnight ("07:30" is 450) and nil. A bad format, something that isn't a number, hours outside 0–23 or minutes outside 0–59 all return 0 and the error "bad time".

Examples
Minutes("07:30")→(450, None)
Minutes("7:5x")→(0, 'bad time')
Minutes("25:00")→(0, 'bad time')
The functionfunc Minutes(clock string) (int, error)
main.go
package main

import (
	"errors"
	"strconv"
	"strings"
)

func Minutes(clock string) (int, error) {
	parts := strings.Split(clock, ":")
	if len(parts) != 2 {
		return 0, errors.New("bad time")
	}
	h, err := strconv.Atoi(parts[0])
	if err != nil {
		return 0, errors.New("bad time")
	}
	m := 0
	// your code here: like the hours part, turn parts[1] into m, returning the error if it fails
	// your code here: hours or minutes out of range? return the error
	return h*60 + m, nil
}
Ctrl + Enter runs too
Result

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