Refuse a negative age and report the message.
Acceptance criteria
- 1.#out should show age cannot be negative.
JavaScript Tutorial · JavaScript Errors
These 10 tasks go with the JavaScript throw chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “throw raises an error deliberately when something is wrong.” Read the chapter.
1.Throw on bad inputexercise · easy · 10 min
Refuse a negative age and report the message.
Passes when: #out should show age cannot be negative.
2.Stop the functionexercise · easy · 10 min · practice plan
Show that nothing after the throw runs.
Passes when: #out should show no value given.
3.Guard a divisionexercise · easy · 10 min · practice plan
Throw when dividing by zero and report the message.
Passes when: #out should show cannot divide by zero.
4.Return rather than throwexercise · easy · 10 min · practice plan
A missing user is normal, so return null instead.
Passes when: #out should show nobody found.
5.Rethrow with contextexercise · medium · 10 min · practice plan
Catch an error and throw a clearer one.
Passes when: #out should show loading failed.
6.Fix the string thrownbug fix · easy · 8 min
A thrown string has no message property.
Passes when: #out should show age cannot be negative.
7.Fix the missing newbug fix · easy · 8 min · practice plan
Error must be created with new to be thrown properly.
Passes when: #out should show it broke.
8.Fix the value returned instead of thrownbug fix · easy · 8 min · practice plan
Returning a message lets the caller ignore the problem.
Passes when: #out should show cannot divide by zero.
9.Fix the throw for a normal outcomebug fix · easy · 8 min · practice plan
A missing user is expected, so it should not throw.
Passes when: #out should show nobody found.
10.Fix the missing validationbug fix · easy · 8 min · practice plan
Without a check the text is coerced and a wrong answer comes back.
Passes when: #out should show width must be a number.
Between them these tasks cover throw, Error objects and Validation.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.