Exercise · Errors · lesson 8 of 10
Exercise: parse an age
Write a function function parseAge(text) that gets an age as a string and returns it as a number. If text is not a number, the function throws a TypeError. If the number is less than 0 or greater than 150, it throws a RangeError.
parseAge("42")→42parseAge("abc")→ throws aTypeErrorparseAge("151")→ throws aRangeError
Constraints: text is a string with no spaces: either a whole number in digits, possibly with a - sign in front, or not a number — an empty string (even though Number("") is 0), letters, or digits mixed with letters ("12a"); the limits 0 and 150 are allowed.
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.