Skip to content
Exercise · Errors · lesson 10 of 11

Exercise: parse an age

Write a function func parseAge(s string) (int, error) that turns a string into an age. If the string is an integer from 0 to 150, the function returns that number and nil. Otherwise it returns 0 and an error that is not nil.

  • parseAge("42") → 42, nil
  • parseAge("abc") → 0 and an error
  • parseAge("151") → 0 and an error

Constraints: the string may be empty or not a number; a negative number and a number above 150 are errors too, while 0 and 150 are valid ages.

Do not add extra imports unless the task says so explicitly.

parseage.go — go
go · 6 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way