Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Validate with Thrown Errors

Asynchronous JavaScriptmedium

Course · Section 16: Asynchronous JavaScript: Promises, Async/Await, and AJAX · Lecture 275: Error Handling With try...catch

Implement `checkAge(age)` returning `ok` when `age` is between 0 and 120 inclusive, otherwise `invalid`. Use a thrown error and a catch internally.

Sample tests

Input: checkAge(25)
Output: "ok"
Input: checkAge(-5)
Output: "invalid"

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Returning a boolean instead of the expected strings.

Learning resources

  • MDN: try...catch
Approach & explanation (try first)

Validation via thrown errors and a catch maps in-range ages to 'ok' and the rest to 'invalid'.

Loading...
⌘/Ctrl + Enter

Run your code to see results.