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, nilparseAge("abc")→0and an errorparseAge("151")→0and 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.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.