The till receives orders like "tea x3". ParseOrder(line) returns the name, the quantity and nil; trimming the spaces and returning "bad order" for a bad quantity are already in the starter. You only need to cut line at " x" into name and qty.
ParseOrder("tea x3")→('tea', 3, None)ParseOrder(" cake x12 ")→('cake', 12, None)ParseOrder("juice")→('', 0, 'bad order')func ParseOrder(line string) (string, int, error)package main
import (
"errors"
"strconv"
"strings"
)
func ParseOrder(line string) (string, int, error) {
line = strings.TrimSpace(line)
name, qty := "", ""
// your code here: cut line at " x" into name and qty
n, err := strconv.Atoi(qty)
if err != nil || n < 1 || name == "" {
return "", 0, errors.New("bad order")
}
return name, n, nil
}
Run tries the examples. When they look right, Submit.