Parse the text and return null when it fails.
Acceptance criteria
- 1.#out should show nothing parsed.
JavaScript Tutorial · JavaScript Errors
These 10 tasks go with the JavaScript Error Handling Patterns 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: “Good error handling is about deciding where a failure should be dealt with.” .
1.Return a fallbackexercise · easy · 10 min
Parse the text and return null when it fails.
Passes when: #out should show nothing parsed.
2.Fail fast on bad inputexercise · easy · 10 min · practice plan
Check the types at the top and throw immediately.
Passes when: #out should show must be numbers.
3.Use a guard clauseexercise · easy · 10 min · practice plan
Return a default rather than throwing for missing input.
Passes when: #out should show Hello stranger.
4.Add context when rethrowingexercise · easy · 10 min · practice plan
Replace the parse error with a clearer message.
Passes when: #out should show settings are not valid JSON.
5.Handle at the boundaryexercise · medium · 10 min · practice plan
Let the loop fail and catch once at the top.
Passes when: #out should show stopped because step two failed.
6.Fix the missing input checkbug fix · easy · 8 min
With no check the text is coerced and a wrong answer is returned.
Passes when: #out should show must be numbers.
7.Fix the error caught and ignoredbug fix · easy · 8 min · practice plan
Catching without acting hides a genuine bug.
Passes when: #out should show TypeError.
8.Fix the rethrow with no new informationbug fix · easy · 8 min · practice plan
Rethrowing the same message adds nothing.
Passes when: #out should show settings are not valid JSON.
9.Fix the throw where a default would dobug fix · easy · 8 min · practice plan
A missing name is expected, so return a greeting instead.
Passes when: #out should show Hello stranger.
10.Fix the handler inside the loopbug fix · easy · 8 min · practice plan
One handler at the boundary is enough.
Passes when: #out should show stopped because step two failed.
Between them these tasks cover Fail fast, Guard clauses and Rethrowing.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.