Skip to content
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") → 42
  • parseAge("abc") → throws a TypeError
  • parseAge("151") → throws a RangeError

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.

parse-age.js — javascript
javascript · 4 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