Command Palette

Search for a command to run...

Hectal
Phase 13: Errors, Debugging and Testing
JavaScript · Exercise 3 of 5 PreviousNext
Intermediate

Your own ValidationError

Finish the ValidationError class: it must extend Error, have the name ValidationError, and store which field was wrong.

Then write validateUsername(value): it trims the value and returns it if it is 3 to 15 characters long. Otherwise it throws new ValidationError("username", "...") with a helpful message. A value that isn't a string also throws a ValidationError.

What Check tests

  1. ValidationError is a kind of Error
  2. Its name is ValidationError
  3. It keeps the field and the message
  4. validateUsername(" asha ") returns "asha"
  5. Too short or too long throws a ValidationError for username
  6. A non-string throws a ValidationError

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.