Kodewick
Start learning →
Mini challenge←→0 / 70

Read an order

#065Mini challengeStringsErrors7 min

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.

Examples
ParseOrder("tea x3")→('tea', 3, None)
ParseOrder(" cake x12 ")→('cake', 12, None)
ParseOrder("juice")→('', 0, 'bad order')
The functionfunc ParseOrder(line string) (string, int, error)
main.go
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
}
Ctrl + Enter runs too
Result

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