$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · easy · #88

What is the correct fix?

const raw = '{bad json}';
const cfg = JSON.parse(raw);
  1. Wrap the parse in a try/catch block
  2. Wrap the parse in a try/finally block
  3. Call JSON.stringify(raw) before parsing
  4. Compare raw against an empty string
JSON.parse throws on malformed input; catch it instead of crashing.
Play Problem Solving ▸