Command Palette

Search for a command to run...

Hectal
Phase 13: Errors, Debugging and Testing
JavaScript · Exercise 2 of 5 PreviousNext
Beginner

Parse JSON without crashing

Saved data can be broken. Write safeParse(text, fallback):

1. Return JSON.parse(text) when the text is valid JSON.

2. When it isn't, log a warning that starts with Bad JSON (use console.warn) and return fallback.

Careful: valid JSON such as "0" or "false" must return 0 or false, not the fallback.

What Check tests

  1. Valid JSON is parsed
  2. Broken JSON doesn't throw
  3. Broken JSON returns the fallback
  4. A warning starting with Bad JSON is logged
  5. Valid 0 and false are kept

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.